读书人

spring主要注解浅析

发布时间: 2012-12-28 10:29:05 作者: rapoo

spring重要注解浅析

一:@PostConstruct(JSR-250)

?

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

Java代码?
?
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()来访问该属性了。

?

我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。
但是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,
这就是<context:annotation-config />:

Java代码?
?
<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>

?

<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

?

二:使用Spring注解完成Bean的定义? 我们推荐使用@Repository(数据存储层)、@Service(业务层)、@Controller(表现层)来替代@Component。

?

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

Java代码?
?
<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>元素定义Bean的配置内容已经被移除,仅需要添加一行<context:component-scan />配置就解决所有问题了——Spring XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan />的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:

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

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

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

?

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


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

Java代码?
@Scope("session")??
@Component()??
public class UserSessionBean implements Serializable {??
??? ...??
}?

读书人网 >编程

热点推荐