spring使用注解@PostConstruct的xml配置
2012-07-07 22:55
369 查看
Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
而我们前面所介绍的AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是 <context:annotation-config/>。请看下面的配置:
清单 19. 调整 beans.xml 配置文件
<?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="boss" class="com.baobaotao.Boss"/> <bean id="office" class="com.baobaotao.Office"> <property name="officeNo" value="001"/> </bean> <bean id="car" class="com.baobaotao.Car" scope="singleton"> <property name="brand" value=" 红旗 CA72"/> <property name="price" value="2000"/> </bean> </beans>
<context:annotationconfig/> 将隐式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor以及equiredAnnotationBeanPostProcessor这 4 个 BeanPostProcessor。
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。
相关文章推荐
- Spring使用AspectJ注解和XML配置实现AOP
- spring框架,可以没有spring.xml(即配置<beans>的文件吗?即不使用xml方式配置,而是只使用注解的方式配置
- 当Spring同时使用了XML配置和注解
- Spring系列之五:使用注解代替xml配置
- spring全注解配置(不使用xml配置)
- Spring XML配置--使用注解装配(@Atutowired、@Inject、@Resource)
- Springboot 中同时使用mybatis注解和springbean-xml配置方式
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring使用注解代替XML配置
- hibernate3使用注解,spring配置文件applicationContext.xml的配置
- spring aop的使用(注解方式以及基于xml配置方式)
- 简化Spring的XML配置(二)——使用注解装配bean
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- spring注解(三)使用注解代替xml配置
- Spring中使用注解代替xml配置文件
- (笔记)Spring实战_最小化Spring XML配置(2)_使用注解装配
- Spring通过XML配置c3p0连接池和dao层注解注入使用 jdbcTemplate
- Spring XML配置--使用注解装配(@Atutowired、@Inject、@Resource)