码迷,mamicode.com
首页 > 编程语言 > 详细

spring annotation功能备注

时间:2017-04-17 11:28:09      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:target   base   play   spring   running   title   isp   告诉   类的方法   

 
@Autowired
@Autowired 注释可以在 setter 方法中被用于自动连接 bean。以type方式进行匹配。
一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接。
 

映射方式1    对变量使用@Autowired,在xml中注入其他bean,可以在注入对象中省略配置类的成员变量

映射方式2    对set方法使用@Autowired,在xml中注入其他bean,可以在注入对象中省略配置类的成员变量

映射方式3    对构造函数使用@Autowired,在xml中注入其他bean,可以在注入对象中省略配置类的成员变量

 

使用时需要配合

<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

@Resource
作用等同@Autowired ,以name方式进行匹配
 关于@Autowired 和@Resource的解释,比较好的一个参考http://www.cnblogs.com/xrq730/p/5313412.html
 
@Required
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。
 
@Component
This annotation tells the spring container to pick the bean while component-scanning is running.
 
技术分享
@Repository
应用于DAO的implement,可以通过添加@Repository,实现自动扫描添加DAO的bean
@Service
应用于service(通常一个service是对一个DAO的服务抽象,直接调用DAO实现类的方法)的implement,可以通过添加@service,实现自动扫描添加service的bean
应用@Repository@Service参考
使用时需要配合<context:component-scan base-package="javabeat.net" />
http://javabeat.net/spring-annotations-component/
@Controller
 作用:在springMVC应用中,告诉服务器,这个类是MVC中的controller,这个类可以接收用户请求处理用户请求
 
@Qualifier
接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类
 
其他的一些注解在其他篇幅介绍
@Import
@PostConstruct
@PreDestroy
@Target
@Retention
@Configuration
@Bean
@Scope
 
 参考http://blog.csdn.net/zhang854429783/article/details/6785574
 
 

spring annotation功能备注

标签:target   base   play   spring   running   title   isp   告诉   类的方法   

原文地址:http://www.cnblogs.com/sylarmeng/p/6722000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!