自己实现spring的注解扫描处理功能

来源:互联网 发布:网络音响哪款好用 编辑:程序博客网 时间:2024/04/29 23:04

Spring注解

1.准备工作
(1)导入common-annotations.jar
(2)导入schema文件 文件名为spring-context-2.5.xsd
(3)xmlbeans节点中配置


2.xml配置工作

 

[html] view plaincopy
  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. default-default-lazy-init="true">  
  10.  <!--将针对注解的处理器配置好  -->     
  11.  <context:annotation-config />     
  12.  <!-- 使用annotation定义事务-->  
  13. <tx:annotation-driventransaction-managertx:annotation-driventransaction-manager="transactionManager" proxy-target-class="true"/>  
  14.  <!--使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入base-package为需要扫描的包(含所有子包)-->  
  15. <context:component-scanbase-packagecontext:component-scanbase-package="com" />  
  16.  .....     
  17.  <beans>    


注:<context:component-scan base-package="*.*" />
 该配置隐式注册了多个对注解进行解析的处理器,如:
 AutowiredAnnotationBeanPostProcessor     
 CommonAnnotationBeanPostProcessor
 PersistenceAnnotationBeanPostProcessor   
 RequiredAnnotationBeanPostProcessor
 其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器,其中就包括了<context:annotation-config/>配置项里面的注解所使用的处理器,所以配置了<context:component-scanbase-package="">之后,便无需再配置<context:annotation-config>


1.在java代码中使用@Autowired@Resource注解方式进行装配 ,这两个注解的区别是:@Autowired默认按类型装配,@Resource默认按名称装配,当找不到名称匹配的bean才会按类型装配。
@Autowired一般装配在set方法之上,也可以装配在属性上边,但是在属性上边配置,破坏了java的封装,所以一般不建议使用

@Autowired是根据类型进行自动装配的。如果当Spring上下文中存在不止一个所要装配类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在所要装配类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。

[java] view plaincopy
  1. @Autowired    
  2. public void setUserDao(@Qualifier("userDao") UserDao userDao) {     
  3.    this.userDao = userDao;     
  4. }    

这样,Spring会找到iduserDaobean进行装配。

可能不存在UserDao实例

[java] view plaincopy
  1. @Autowired(required = false)     
  2. public void setUserDao(UserDao userDao) {     
  3.     this.userDao = userDao;     
  4. }    


2.@ResourceJSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解)Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource@PostConstruct以及@PreDestroy
@Resource的作用相当于@Autowired,只不过@AutowiredbyType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是nametypeSpring@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略
@Resource装配顺序

1 如果同时指定了nametype,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

2 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

3 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

4 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;


3. @PostConstructJSR-250
在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。
它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时,如:

[java] view plaincopy
  1. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {     
  2.  private SessionFactory mySessionFacotry;     
  3. @Resource    
  4. public void setMySessionFacotry(SessionFactory sessionFacotry) {     
  5.     this.mySessionFacotry = sessionFacotry;     
  6.    }     
  7.   @PostConstruct    
  8.    public void injectSessionFactory() {     
  9.       super.setSessionFactory(mySessionFacotry);     
  10.     }   }    



这里通过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了我们自己定义的sessionFactory(父类的setSessionFactory方法为final,不可复写),之后我们就可以通过调用super.getSessionFactory()来访问该属性了。
4.@PreDestroyJSR-250
在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。由于我们当前还没有需要用到它的场景,这里不不去演示。其用法同@PostConstruct
5.使用Spring注解完成Bean的定义
以上我们介绍了通过@Autowired@Resource来实现在Bean中自动注入的功能,下面我们将介绍如何注解Bean,从而从XML配置文件中完全移除Bean定义的配置。
@Component需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:

[java] view plaincopy
  1. @Component    
  2. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {     
  3.     ...     
  4. }    

使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是userDaoImpl。你也可以指定Bean的名称:
@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository@Service@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository@Service@Controller来替代@Component

6.使用<context:component-scan />Bean定义注解工作起来


  1. <beans xmlns="http://www.springframework.org/schema/beans"   
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xmlns:context="http://www.springframework.org/schema/context"        
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans      
  5.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  6.   http://www.springframework.org/schema/context       
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">    
  8.    <context:component-scan base-package="com.bzu" />     
  9. </beans>  
0 0