Spring的注解方式 No.5

Spring 的注解方式:

@Autowired 注解 (不推荐使用,建议使用@Resource)

@Autowired 可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。

@Autowired 的标注位置不同,它们都会在 Spring 在初始化这个 bean 时,自动装配这个属性。要使@Autowired 能够工作,还需要在配置文件中加入以下

Xml 代码

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

* @Qualifier 注解*

@Autowired 是根据类型进行自动装配的。

例如,如果当 Spring 上下文中存在不止一个
UserDao 类型的 bean 时,就会抛出 BeanCreationException 异常;如果 Spring 上下文中不
存在 UserDao 类型的 bean,也会抛出 BeanCreationException 异常。我们可以使用
@Qualifier 配合@Autowired 来解决这些问题。如下:

1). 可能存在多个 UserDao 实例

 @Autowired
 @Qualifier("userServiceImpl")
 public IUserService userService;

或者

 @Autowired
 public void setUserDao(@Qualifier("userDao") UserDao userDao) {
 this.userDao = userDao; }

这样,Spring 会找到 id 为 userServiceImpl 和 userDao 的 bean 进行装配。

2). 可能不存在 UserDao 实例

@Autowired(required = false)
public IUserService userService;

@Resource 注解

JSR-250 标准注解,推荐使用它来代替 Spring 专有的@Autowired 注解。
@Resource 的作用相当于@Autowired,只不过@Autowired 按 byType 自动注入,而@Resource 默认按 byName
自动注入罢了。

@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将@Resource 注解的 name 属性解析为 bean 的名字,而 type 属性则解析为 bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName自动注入策略。要使@Autowired 能够工作,还需要在配置文件中加入以下:

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

@Resource 装配顺序:

a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
b.如果指定了 name,则从上下文中查找名称(id)匹配的 bean 进行装配,找不到则抛出异常
c.如果指定了 type,则从上下文中找到类型匹配的唯一 bean 进行装配,找不到或者找到多个,都会抛出异常
d.如果既没有指定 name,又没有指定 type,则自动按照 byName 方式进行装配(见 2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;

@PostConstruct(JSR-250)注解

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

public class UserDaoImpl extends HibernateDaoSupport implements UserDao
{

private SessionFactory mySessionFacotry;

@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry)
{
this.mySessionFacotry = sessionFacotry;
}

@PostConstruct
public void injectSessionFactory()
{
super.setSessionFactory(mySessionFacotry);
    }
}

这里通过@PostConstruct,为 UserDaoImpl 的父类里定义的一个 sessionFactory 私有
属性,注入了我们自己定义的 sessionFactory(父类的 setSessionFactory 方法为 final,
不可复写),之后我们就可以通过调用 super.getSessionFactory()来访问该属性了。

@PreDestroy(JSR-250)注解

在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。
其用法同@PostConstruct。和@PostConstruct 区别在于:@PostConstruct 注释的方法将在
类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。

@Component 注解 (不推荐使用)

只需要在对应的类上加上一个@Component 注解,就将该类定义为一个 Bean 了。Spring
还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储
层 Bean,业务层 Bean,和展示层 Bean。目前版本(2.5)中,这些注解与@Component 的语义
是一样的,完全通用,在 Spring 以后的版本中可能会给它们追加更多的语义。所以,我们
推荐使用@Repository、@Service、@Controller 来替代@Component。

@Scope 注解

在使用 XML 定义 Bean 时,我们可能还需要通过 bean 的 scope 属性来定义一个 Bean 的
作用范围,我们同样可以通过@Scope 注解来完成这项工作:

@Scope("session")
@Component()
public class UserSessionBean implements Serializable{
......
}

二、配置启用注解(注意以下配置需要使用 spring2.5 的头文件,在 spring3.0 中不适
用)
1.使用简化配置
Spring2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属
性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,
它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工
作起来。
AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor 就
是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨
拙。Spring 为我们提供了一种方便的注册这些 BeanPostProcessor 的方式,这就是,以下
是 spring 的配置。

Xml 配置约束 添加开启注解

<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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
beans>

将隐式地向 Spring 容器注册了

AutowiredAnnotationBeanPostProcessor 
CommonAnnotationBeanPostProcessor 
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor

这 4 个 BeanPostProcessor。

使用让 Bean 定义注解工作起来

<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.kedacom.ksoa" />
beans>

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

还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方
式:

过滤器类型      | 表达式范例                                      | 说明
注解             | org.example.SomeAnnotation                 | 将所有使用 SomeAnnotation 注解的类过滤出来
类名指定           | org.example.SomeClass                      | 过滤指定的类
正则表达式      | com\.kedacom\.spring\.annotation\.web\..*     | 通过正则表达式过滤一些类
AspectJ 表达式  | org.example..*Service+                       | 通过 AspectJ 表达式过滤一些类

以正则表达式为例,我列举一个应用实例:

<context:component-scan base-package="com.casheen.spring.annotation">
<context:exclude-filter type="regex"
expression="com\.casheen\.spring\.annotation\.web\..*" />
context:component-scan>

值得注意的是配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,
同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor),因此
当使用后,就可以将移除了。

是不支持 spring 的@Transcation 和 EJB 的 Spring’s @Transactional or EJB3’s
@TransactionAttribute annotation。用此配置可以达到目的。

使用@Scope 来定义 Bean 的作用范围

在使用 XML 定义 Bean 时,我们可能还需要通过 bean 的 scope 属性来定义一个 Bean 的
作用范围,我们同样可以通过@Scope 注解来完成这项工作:

@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
 ...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值