Sping注解入门

Posted on 2010-06-25 17:05 FaithChen 阅读(1867) 评论(0)  编辑  收藏

  1.让@Autowired工作起来的准备工作

要使@Autowired能够工作,首先需要在配置文件中加入以下代码

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

 说明@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置可以不同,这将在下文中说明,它们都会在Spring在初始化bean时,自动装配@Autowired指定的属性,注意,@Autowired是通过类型装配的,而非id,当然,可以@Qualifier配合@Autowired来解决使用id进行装配,并解决当Spring上下文中受装配bean不存在的问题,这些将在下文中说明

2. 使用Spring注解来注入属性
2.1. 使用注解以前我们是怎样注入属性的
类的实现:

public class UserManagerImpl implements UserManager {
    
private
 UserDao userDao;
    
public void setUserDao(UserDao userDao) 
{
        
this.userDao =
 userDao;
    }

    
}

配置文件:

<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
    
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
    
<property name="sessionFactory" ref="mySessionFactory" />
</bean>


2.2. 引入@Autowired注解(不推荐使用,建议使用@Resource)
类的实现(对成员变量进行标注)


或者(对方法进行标注)


配置文件

以上两种不同实现方式中,@Autowired的标注位置不同,它们都会在Spring在初始化userManagerImpl这个bean时,自动装配userDao这个属性,区别是:第一种实现中,Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;第二种实现中,Spring会调用setUserDao方法来将UserDao类型的唯一一个bean装配到userDao这个属性。

3. @Qualifier
@Autowired是根据类型进行自动装配的。在上面的例子中,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。
3.1. 可能存在多个UserDao实例
这样,Spring会找到id为userDao的bean进行装配。
3.2. 可能不存在UserDao实例

5. @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()来访问该属性了。

7. @PreDestroy(JSR-250)
在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。由于我们当前还没有需要用到它的场景,这里不不去演示。其用法同@PostConstruct。

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

<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 />
</beans>
PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

9. 使用Spring注解完成Bean的定义

以上我们介绍了通过@Autowired或@Resource来实现在Bean中自动注入的功能,下面我们将介绍如何注解Bean,从而从XML配置文件中完全移除Bean定义的配置。

只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao 
{
    
}

@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。

<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。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>

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

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

 

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

 



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


网站导航:
 

posts - 4, comments - 0, trackbacks - 0, articles - 4

Copyright © FaithChen