spring annotation功能备注

 
@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"/>

或者使用

<context:annotation-config/> ,它包含了前者隐式注入
@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.
 
image
@Repository
应用于DAO的implement,可以通过添加@Repository,实现自动扫描添加DAO的bean
@Service
应用于service(通常一个service是对一个DAO的服务抽象,直接调用DAO实现类的方法)的implement,可以通过添加@service,实现自动扫描添加service的bean
应用@Repository@Service参考
使用时需要配合<context:component-scan base-package="javabeat.net" />
component-scan包含了<context:annotation-config/> 
http://javabeat.net/spring-annotations-component/
@Controller
 作用:在springMVC应用中,告诉服务器,这个类是MVC中的controller,这个类可以接收用户请求处理用户请求
需要配合
<mvc:annotation-driven/>
使用
 
@Qualifier
接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类
 
其他的一些注解在其他篇幅介绍
@Import
@PostConstruct
@PreDestroy
@Target
@Retention
@Configuration
@Bean
@Scope
 
 参考http://blog.csdn.net/zhang854429783/article/details/6785574
 
 

posted on 2017-04-17 11:23  猫不白  阅读(228)  评论(0编辑  收藏  举报

导航