一:@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 {
...
}
分享到:
相关推荐
spring3 注解 详解spring3 注解 详解spring3 注解 详解
使用Spring的注解方式实现AOP的细节
Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解 Spring java注解,元注解和自定义注解
spring源码带注释的,直接可以阅读,不需要重新编译。
spring源码(注释+测试版)
这是对Spring中注解是怎么实现的一个大概基本原理,条件是采取的理想状态,所以代码中还有缺陷的话请谅解,如果有需要的朋友可以放心下载,里面有详细的解释和流程。相信你能看懂
spring3注解详细
Spring demo 自动检测注解
简单的demo4初学者,用到spring注解,log4j记录日志到文件
Spring通过注解实现IOC,Spring通过注解实现IOC,Spring通过注解实现IOC
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
spring自定义注解样例
Spring注解
spring4注解
Spring 常用注解整理,分类:创建对象;注入数据;范围;全局异常;生命周期;新注解;JPA;扩展原理等注解类型。
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
spring 源码解析
关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用
spring源码 中英文注释
SpringJDBC注解事务.zip,SpringJDBC注解事务.zip