posts - 495,comments - 227,trackbacks - 0

spring 注释功相当于

以前在spring的配置文件的bean头中加入了:

default-autowire="byName"

default-autowire="byType"

 

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

 

@Autowired  默让是byType

它可以注释到方法,也可以注释到成员域:

如下两种:

1、

package com.web.userManagser.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.web.userManagser.services.UserManagerService;
public class UserManagerAction {
@Autowired
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
}

 2、

package com.web.userManagser.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.web.userManagser.services.UserManagerService;
public class UserManagerAction {
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
public UserManagerService getUserManagerService() {
return userManagerService;
}
@Autowired
public void setUserManagerService(UserManagerService userManagerService) {
this.userManagerService = userManagerService;
}
}

 

 

当候选 Bean 数目不为1个时个时的应对方法

1、bean 数目为0个时:

 

也就是spring 找不到一个匹配的bean, spring 就会抛出BeanCreationException 异常

 

如上述的情况怎么解决呢。如果想让spring 找不到bean 不报错那么你在注释的时候可以如下的注释方法:

@Autowired(required = false)

当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。

 

 

2、bean 数目不只一个,而是多个时呢?

如果我们在spring 中配置了两个相同的类型时,而我们的注释又同时是通过byType时

那么spring 就会出错,它不知道哪个bean 是它要的。

那么我们就可以将@Qualifier 和@Autowired 一起用

 

用的同时就是把自动配置的方式从byType 转向 byName,

如下使用:

 

配置文件中有:

<bean id="userManagerService" class="com.web.userManagser.services.UserManagerService"/>
<bean id="userManagerAction" class="com.web.userManagser.action.UserManagerAction"/>
<bean id="userManagerAction1" class="com.web.userManagser.action.UserManagerAction"/>

 

 

此时我们就可以:

public class UserManagerAction {
@Autowired
@Qualifier("userManagerService")
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
}

 

@Qualifier 只能和 @Autowired 结合使用,是对 @Autowired 有益的补充。一般来讲,@Qualifier 对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。

 

 

 我们还可以用使用 JSR-250 的注释

 

要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 该 BeanPostProcessor 将自动起作用,对标注的 @Resource、@PostConstruct 以及 @PreDestroy-->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

<!-- 移除 boss Bean 的属性注入配置的信息 -->
<bean id="userManagerService" class="com.web.userManagser.services.UserManagerService"/>
<bean id="userManagerService1" class="com.web.userManagser.services.UserManagerService"/>
<bean id="userManagerAction" class="com.web.userManagser.action.UserManagerAction"/>
</beans>

 

 

@Resource@PostConstruct 以及 @PreDestroy

 

 1、@Resource 有两个属性。

  name spring 将它解释成bean的名字

  type spring 将它解释成bean 的类型

 

如果我们定义了name那么spring 就使用byName 方式注入

如果我们定义了type 那么spring 就用byTyte方式注入

如果两者都没有定义那么就默为byName

 

@PostConstruct 和 @PreDestroy

 

这两个注释定义在方法上,

被@PostConstruct 注释的方法,bean 被实例化后调用的。

而被@PreDestroy 注释的方法,bean 被销毁时调用的。

public class UserManagerAction {
@Resource(name="userManagerService")
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
@PostConstruct
public void ct(){
System.out.println("ct");
}
@PreDestroy
public void pro(){
System.out.println("ct");
}
}

 

 

我们知道,不管是通过实现 InitializingBean/DisposableBean 接口,还是通过 <bean> 元素的 init-method/destroy-method 属性进行配置,都只能为 Bean 指定一个初始化 / 销毁的方法。但是使用 @PostConstruct@PreDestroy 注释却可以指定多个初始化 / 销毁方法,那些被标注 @PostConstruct@PreDestroy 注释的方法都会在初始化 / 销毁时被执行。

 

 

 

使用 <context:annotation-config/> 简化配置

 

Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

而我们前面所介绍的 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor 就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些 BeanPostProcessor 的方式,这就是 <context:annotation-config/>。

 

<?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/>
<bean id="userManagerService" class="com.web.userManagser.services.UserManagerService"/>
<bean id="userManagerAction" class="com.web.userManagser.action.UserManagerAction"/>
</beans>

 

<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。

在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

 

 

 

使用 @Component

虽然我们可以通过 @Autowired@Resource 在 Bean 类中使用自动注入功能,但是 Bean 还是在 XML 文件中通过 <bean> 进行定义 —— 也就是说,在 XML 配置文件中定义 Bean,通过 @Autowired@Resource 为 Bean 的成员变量、方法入参或构造函数入参提供自动注入的功能。能否也通过注释定义 Bean,从 XML 配置文件中完全移除 Bean 定义的配置呢?答案是肯定的,我们通过 Spring 2.5 提供的 @Component 注释就可以达到这个目标了。

 

下面,我们完全使用注释定义 Bean 并完成 Bean 之间装配:

package com.web.userManagser.services;
import org.springframework.stereotype.Component;
@Component
public class UserManagerService {
public void sayHello(){
System.out.println("fuck you!");
}
}

 

@Component("userManagerAction")
public class UserManagerAction {
@Resource(name="userManagerService")
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
}

 

@Component 有一个可选的入参,用于指定 Bean 的名称,在 Boss 中,我们就将 Bean 名称定义为“boss”。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。

在使用 @Component 注释后,Spring 容器必须启用类扫描机制以启用注释驱动 Bean 定义和注释驱动 Bean 自动注入的策略。Spring 2.5 对 context 命名空间进行了扩展,提供了这一功能,请看下面的配置:

 

 

<?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="com.web"/>
</beans>

 

 

这里,所有通过 <bean> 元素定义 Bean 的配置内容已经被移除,仅需要添加一行 <context:component-scan/> 配置就解决所有问题了——Spring XML 配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

 

<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式,通过下表说明:


表 1. 扫描过滤方式

过滤器类型 说明
注释 假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。
类名指定 通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。
正则表达式 通过正则表达式定义过滤的类,如下所示: com\.baobaotao\.Default.*
AspectJ 表达式 通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+

下面是一个简单的例子:

 

<context:component-scan base-package="com.web">
             <context:include-filter type="aspectj" expression="com.web..action..*"/>
            </context:component-scan>
            

 

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

通过 @Scope 指定 Bean 的作用范围

              
package com.baobaotao;
import org.springframework.context.annotation.Scope;

@Scope("prototype")
@Component("boss")
public class Boss {
    …
}

这样,当从 Spring 容器中获取 boss Bean 时,每次返回的都是新的实例了。

 

 

 

采用具有特殊语义的注释

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository@Service@Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository@Service@Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。

 

 

 

注释配置和 XML 配置的适用场合

是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因:

  • 注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。
  • 如果 Bean 不是自己编写的类(如 JdbcTemplateSessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。
  • 注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于 @Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。

所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。

posted on 2010-12-15 22:05 SIMONE 阅读(513) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: