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

spring annotation

 
阅读更多

Spring3.1中使用注解装配属性
可在Java代码中使用@Resource或者@Autowired注解进行装配,但需在XML中配置以下信息

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
    default-autowire="byName">


然后显式的配置<context:annotation-config/>
该配置隐式注册了多个对注解进行解析的处理器,如下列举
AutowiredAnnotationBeanPostProcessor     

CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor   

RequiredAnnotationBeanPostProcessor

<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

 


成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean。

<context:component-scan base-package="org.bdp.system.test.controller">   

   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   </context:component-scan>

不仅仅扫描@Controller,还扫描@Service/@Repository的Bean,可能造成一些问题

<context:component-scan base-package="org.bdp">   

   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>

事务不起作用

1 <context:component-scan>会交给org.springframework.context.config.ContextNamespaceHandler处理;

registerBeanDefinitionParser("component-scan"new ComponentScanBeanDefinitionParser());

 

2、ComponentScanBeanDefinitionParser会读取配置文件信息并组装成org.springframework.context.annotation.ClassPathBeanDefinitionScanner进行处理;

3、如果没有配置<context:component-scan>的use-default-filters属性,则默认为 true,在创建ClassPathBeanDefinitionScanner时会根据use-default-filters是否为true来调用如 下代码:

protected void registerDefaultFilters() {  this.includeFilters.add(new AnnotationTypeFilter(Component.class));  ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); 

try {   this.includeFilters.add(new AnnotationTypeFilter(   ((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));   logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning"); 

catch (ClassNotFoundException ex) {   // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.try {  this.includeFilters.add(new AnnotationTypeFilter(              ((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));      logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");

catch (ClassNotFoundException ex) {     // JSR-330 API not available - simply skip. 


 

可以看到默认ClassPathBeanDefinitionScanner会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描。如果细心,到此我们就找到问题根源了。

 

 

4、在进行扫描时会通过include-filter/exclude-filter来判断你的Bean类是否是合法的:

 

  1. protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {  
  2.     for (TypeFilter tf : this.excludeFilters) {  
  3.         if (tf.match(metadataReader, this.metadataReaderFactory)) {  
  4.             return false;  
  5.         }  
  6.     }  
  7.     for (TypeFilter tf : this.includeFilters) {  
  8.         if (tf.match(metadataReader, this.metadataReaderFactory)) {  
  9.             AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();  
  10.             if (!metadata.isAnnotated(Profile.class.getName())) {  
  11.                 return true;  
  12.             }  
  13.             AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);  
  14.             return this.environment.acceptsProfiles(profile.getStringArray("value"));  
  15.         }  
  16.     }  
  17.     return false;  

 

首先通过exclude-filter 进行黑名单过滤;

然后通过include-filter 进行白名单过滤;

否则默认排除。

如果在springmvc配置文件,不使用cn.javass.demo.web.controller前缀,而是使用cn.javass.demo,则 service、dao层的bean可能也重新加载了,但事务的AOP代理没有配置在springmvc配置文件中,从而造成新加载的bean覆盖了老的 bean,造成事务失效。只要使用use-default-filters=“false”禁用掉默认的行为就可以了

其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器

另外,比较建议使用@Resource注解,而不要使用@Autowired注解
因为@Autowired注解是Spring提供的,而@Resource注解是J2EE提供的
在JDK6中就已经包含@Resource注解了,所以它没有跟Spring紧密耦合
并且在使用Spring时,若使用了JSR-250中的注解,

如@Resource//@PostConstruct//@PreDestroy
那么还需要Spring安装目录中的SPRING_HOME\\lib\\j2ee\\common-annotations.jar包的支持
这里面的@Resource注解就是在SPRING_HOME\\lib\\j2ee\\common-annotations.jar中的

@Resource注解
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上
@Resource默认按名称装配,名称可以通过name属性指定。当找不到与名称匹配的bean时,才会按类型装配
若注解标注在字段上且未指定name属性,则默认取字段名作为bean名称寻找依赖对象
若注解标注在setter上且未指定name属性,则默认取属性名作为bean名称寻找依赖对象
如果没有指定name属性,并且按照默认的名称仍找不到依赖对象时,它就会按类型匹配
但只要指定了name属性,就只能按名称装配了

@Autowired注解
@Autowired默认是按类型装配对象的,默认情况下它要求依赖对象必须存在
如果允许null值,可以设置它的required属性为FALSE,如@Autowired(required=false)
若想要按名称装配,可以结合@Qualifier注解一起使用,如@Autowired(required=false)  @Qualifier("personDaoBean")


--------------------------------------------------------------------------------

Spring23.1的组件自动扫描
在一个稍大的项目中通常会有上百个组件,如果都使用XML的bean定义来配置组件的话
显然会增加配置文件的体积,查找及维护也不方便
而Spring3.1就为我们引入了组件自动扫描机制
它可以在classpath下寻找标注了@Service、@Repository、@Controller、@Component注解的类
并把这些类纳入Spring容器中管理,它的作用和在XML中使用bean节点配置组件是一样的
使用自动扫描机制,则需配置<context:component-scan base-package="com.jadyer"/>启动自动扫描
其中base-package指定需要扫描的包,它会扫描指定包中的类和子包里面类
@Service用于标注业务层组件
@Repository用于标注数据访问组件,即DAO组件
@Controller用于标注控制层组件,如Struts中的Action
@Component泛指组件,当组件不要好归类时,可以使用这个注解进行标注

1、可以使用诸如@Service("personDao")修改bean名称,而它默认的是将首字母小写的类名作为<bean>名称
2、若要更改<bean>作用域的话,可以使用@Scope("prototype")注解来修改<bean>作用域
3、若想让<bean>实例化之后去执行初始化方法,可以使用@PostConstruct标注在方法上
4、同样@PreDestroy注解标注在方法上,可以用来指定<bean>销毁时执行的方法
这里的@PostConstruct是EJB3里面用来初始化bean的注解,它也不是Spring中的注解
并且<context:component-scan base-package=""/>的背后注册了很多用于解析注解的处理器
其中就包括了<context:annotation-config/>配置项里面的注解所使用的处理器
所以配置了<context:component-scan base-package="">之后,便无需再配置<context:annotation-config>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics