`
liuxinglanyue
  • 浏览: 548428 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring Annotation 笔记——IOC篇

阅读更多

@Autowired 
1、Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。 

Java代码 
  1. <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->  
  2. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>       

或者使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。) 
Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"             
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             
  4. xmlns:context="http://www.springframework.org/schema/context"             
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7. http://www.springframework.org/schema/context                 
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.   
  10. <context:annotation-config/>   
  11. </beans>  

2、@Autowired默认按照类型匹配的方式进行注入 
3、@Autowired注解可以用于成员变量、setter方法、构造器函数等 
4、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常 
5、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。 
Java代码 
  1. public class MovieRecommender {  
  2.   
  3. @Autowired  
  4. @Qualifier("mainCatalog")  
  5. private MovieCatalog movieCatalog;  
  6.       
  7.     private CustomerPreferenceDao customerPreferenceDao;  
  8.   
  9.     @Autowired  
  10.     public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {  
  11.         this.customerPreferenceDao = customerPreferenceDao;  
  12.     }  
  13.   
  14.     // ...  
  15. }  



@Resource 
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了。 
2、要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor 
Java代码 
  1. <bean  class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>   

3、@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。 
Java代码 
  1. public class SimpleMovieLister {  
  2.   
  3.     private MovieFinder movieFinder;  
  4.   
  5.     @Resource  
  6.     public void setMovieFinder(MovieFinder movieFinder) {  
  7.         this.movieFinder = movieFinder;  
  8.     }  
  9. }  



@PostConstruct 和 @PreDestroy 
标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。 
Java代码 
  1. public class CachingMovieLister {  
  2.   
  3.     @PostConstruct  
  4.     public void populateMovieCache() {  
  5.         // populates the movie cache upon initialization...  
  6.     }  
  7.       
  8.     @PreDestroy  
  9.     public void clearMovieCache() {  
  10.         // clears the movie cache upon destruction...  
  11.     }  
  12. }  



@Component 
1、使用@Component注解可以直接定义Bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。 
2、@Component 有一个可选的入参,用于指定 Bean 的名称。 
Java代码 
  1. @Component  
  2. public class ActionMovieCatalog implements MovieCatalog {  
  3.     // ...  
  4. }  

3、<context:component-scan/> 允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式: 
过滤器类型 表达式范例
annotation org.example.SomeAnnotation
assignable org.example.SomeClass
regex org\.example\.Default.*
aspectj org.example..*Service+

下面这个XML配置会忽略所有的@Repository注解并用“stub”储存库代替。 
Java代码 
  1. <beans ...>  
  2.   
  3.      <context:component-scan base-package="org.example">  
  4.         <context:include-filter type="regex" expression=".*Stub.*Repository"/>  
  5.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>  
  6.      </context:component-scan>  
  7.   
  8. </beans>  

4、默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标 
Java代码 
  1. @Scope("prototype")  
  2. @Repository  
  3. public class MovieFinderImpl implements MovieFinder {  
  4.     // ...  
  5. }  

5、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层) 
Java代码 
  1. @Service  
  2. public class SimpleMovieLister {  
  3.   
  4.     private MovieFinder movieFinder;  
  5.   
  6.     @Autowired  
  7.     public SimpleMovieLister(MovieFinder movieFinder) {  
  8.         this.movieFinder = movieFinder;  
  9.     }  
  10. }  
  11.   
  12. @Repository  
  13. public class JpaMovieFinder implements MovieFinder {  
  14.     // implementation elided for clarity  
  15. }  

6、要检测这些类并注册相应的bean,需要在XML中包含以下元素,其中'basePackage'是两个类的公共父包 (或者可以用逗号分隔的列表来分别指定包含各个类的包)。 
Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.                  
  10.      <context:component-scan base-package="org.example"/>  
  11.        
  12. </beans>  


此外,在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。 

转:http://kdboy.iteye.com/blog/419159

分享到:
评论

相关推荐

    Spring学习笔记

    Spring学习笔记Spring spring的配置 IOC 依赖注入 基于Xml的注入 基于注释的注入 Spring的自动注入和属性自动注入 AOP 静态代理 动态代理 使用spring实现AOP 基于Annotation实现AOP 基于XML实现AOP ...

    Spring的学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    spring2.5 学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    spring2.5学习笔记

    搭建sping的运行环境,IOC(DI)配置及应用,annotation方式Spring

    spring框架案例学习文档笔记

    面向抽象编程 Jdom的基本使用 IOC(DI)配置及应用 模拟Spring功能 搭建sping的运行环境 annotation方式Spring Spring整合Hiberante3 Spring整合-SSH DTO、VO SSH整合的jar包 SSH整合存在的问题

    springmybatis

    而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记。后来项目结束了,我也没写总结文档。已经过去好久了。但最近突然又对这个ORM 工具感兴趣。因为接下来自己的项目中很有可能采用这个ORM工具。所以在此...

    Java/JavaEE 学习笔记

    第二章 Spring IOC(控制反转)........347 第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL...

    J2EE学习笔记(J2ee初学者必备手册)

    第二章 Spring IOC(控制反转)........347 第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL...

    java简易版开心农场源码-spring-boot-tut:弹簧靴

    话说我当年接触Spring的时候着实兴奋了好一阵,IoC的概念当初第一次听说,感觉有种开天眼的感觉。记得当时的web框架和如今的前端框架的局面差不多啊,都是群雄纷争。但一晃好多年没写过后端,代码这东西最怕手生,...

    java版商城源码下载-spring-boot-tut:MySpringBootTutorial--Focusingonminimizingt

    话说我当年接触Spring的时候着实兴奋了好一阵,IoC的概念当初第一次听说,感觉有种开天眼的感觉。记得当时的web框架和如今的前端框架的局面差不多啊,都是群雄纷争。但一晃好多年没写过后端,代码这东西最怕手生,...

    mybank

    没有Spring的Java RESTful服务使用的依赖项和插件org.... 创建模型,服务和Web层Servlet接受GET和POST请求目前没有数据库-将JSON存储在列表中接受金额和参考以创建新交易接口ApplicationContext代表Spring IoC容器。 它

Global site tag (gtag.js) - Google Analytics