spring annotation
来源:互联网 发布:淘宝上卖茶叶怎么样 编辑:程序博客网 时间:2024/05/03 11:31
@Autowired
1、Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
或者使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。)
2、@Autowired默认按照类型匹配的方式进行注入
3、@Autowired注解可以用于成员变量、setter方法、构造器函数等
4、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常
5、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
@Resource
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了。
2、要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
3、@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。
@PostConstruct 和 @PreDestroy
标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
@Component
1、使用@Component注解可以直接定义Bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。
2、@Component 有一个可选的入参,用于指定 Bean 的名称。
3、<context:component-scan/> 允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式:
过滤器类型表达式范例annotationorg.example.SomeAnnotationassignableorg.example.SomeClassregexorg\.example\.Default.*aspectjorg.example..*Service+
下面这个XML配置会忽略所有的@Repository注解并用“stub”储存库代替。
4、默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标
5、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)
6、要检测这些类并注册相应的bean,需要在XML中包含以下元素,其中'basePackage'是两个类的公共父包 (或者可以用逗号分隔的列表来分别指定包含各个类的包)。
此外,在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。
1、Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
- <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或者使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
- </beans>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config/> </beans>
2、@Autowired默认按照类型匹配的方式进行注入
3、@Autowired注解可以用于成员变量、setter方法、构造器函数等
4、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常
5、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
- public class MovieRecommender {
- @Autowired
- @Qualifier("mainCatalog")
- private MovieCatalog movieCatalog;
- private CustomerPreferenceDao customerPreferenceDao;
- @Autowired
- public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
- this.customerPreferenceDao = customerPreferenceDao;
- }
- // ...
- }
public class MovieRecommender {@Autowired@Qualifier("mainCatalog")private MovieCatalog movieCatalog; private CustomerPreferenceDao customerPreferenceDao; @Autowired public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { this.customerPreferenceDao = customerPreferenceDao; } // ...}
@Resource
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了。
2、要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
3、@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。
- public class SimpleMovieLister {
- private MovieFinder movieFinder;
- @Resource
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- }
public class SimpleMovieLister { private MovieFinder movieFinder; @Resource public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; }}
@PostConstruct 和 @PreDestroy
标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
- public class CachingMovieLister {
- @PostConstruct
- public void populateMovieCache() {
- // populates the movie cache upon initialization...
- }
- @PreDestroy
- public void clearMovieCache() {
- // clears the movie cache upon destruction...
- }
- }
public class CachingMovieLister { @PostConstruct public void populateMovieCache() { // populates the movie cache upon initialization... } @PreDestroy public void clearMovieCache() { // clears the movie cache upon destruction... }}
@Component
1、使用@Component注解可以直接定义Bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。
2、@Component 有一个可选的入参,用于指定 Bean 的名称。
- @Component
- public class ActionMovieCatalogimplements MovieCatalog {
- // ...
- }
@Componentpublic class ActionMovieCatalog implements MovieCatalog { // ...}
3、<context:component-scan/> 允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式:
过滤器类型表达式范例annotationorg.example.SomeAnnotationassignableorg.example.SomeClassregexorg\.example\.Default.*aspectjorg.example..*Service+
下面这个XML配置会忽略所有的@Repository注解并用“stub”储存库代替。
- <beans ...>
- <context:component-scan base-package="org.example">
- <context:include-filter type="regex" expression=".*Stub.*Repository"/>
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
- </context:component-scan>
- </beans>
<beans ...> <context:component-scan base-package="org.example"> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan></beans>
4、默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标
- @Scope("prototype")
- @Repository
- public class MovieFinderImplimplements MovieFinder {
- // ...
- }
@Scope("prototype")@Repositorypublic class MovieFinderImpl implements MovieFinder { // ...}
5、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)
- @Service
- public class SimpleMovieLister {
- private MovieFinder movieFinder;
- @Autowired
- public SimpleMovieLister(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- }
- @Repository
- public class JpaMovieFinderimplements MovieFinder {
- // implementation elided for clarity
- }
@Servicepublic class SimpleMovieLister { private MovieFinder movieFinder; @Autowired public SimpleMovieLister(MovieFinder movieFinder) { this.movieFinder = movieFinder; }}@Repositorypublic class JpaMovieFinder implements MovieFinder { // implementation elided for clarity}
6、要检测这些类并注册相应的bean,需要在XML中包含以下元素,其中'basePackage'是两个类的公共父包 (或者可以用逗号分隔的列表来分别指定包含各个类的包)。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:component-scan base-package="org.example"/>
- </beans>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="org.example"/> </beans>
此外,在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。
- Spring Annotation
- Spring Annotation
- spring annotation
- Spring annotation
- spring annotation
- spring annotation
- spring annotation
- Spring Annotation
- Spring Annotation
- spring-annotation
- spring-Annotation
- spring annotation
- Java Meta Annotation & Spring Annotation
- Spring 2.5 Annotation
- Spring Annotation is dead
- Spring中的Annotation
- Spring Annotation 详解
- Spring Annotation -- @Autowired, @Qualifier
- eclipse for j2ee配置tomcat
- php自动跳转
- vim单词高亮功能的添加和用法
- matlab矩阵运算
- EXT表格组件GRIDPANEL介绍
- spring annotation
- wxWidgets的光标Caret
- web抓取工作特点
- 背包问题
- Visual Studio项目迁移到Linux系统
- 编程人生
- 用canvas画的图片跟在图片上写字居中的问题?
- 如何读写property文件,保证一定顺序
- MFC 下JSONCPP 的使用 C++ JSON解析