您的位置:首页 > 编程语言 > Java开发

注解的力量 -----Spring&nbsp…

2013-09-11 15:59 239 查看
在(三)里面。我们引入了 <bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>这个bean
来处理@Autowired注解。
其实在spring 里面还有其他三个BeanPostProcessor 。总共有四个,分别是:
AutowiredAnnotationBeanPostProcessor

CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor

equiredAnnotationBeanPostProcessor


但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些
BeanPostProcessor 的方式,这就是
<context:annotation-config/>

Spring 2.1 添加了一个新的 context 的 Schema
命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

这段代码就是 启用了这个命名空间后的applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<bean id="entityManagerFactory"

class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">

<property name="persistenceUnitName" value="testerPU" />

</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionInterceptor"

class="org.springframework.transaction.interceptor.TransactionInterceptor">

<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->

<property name="transactionManager">

<ref local="transactionManager" />

</property>

<property name="transactionAttributes">

<!-- 下面定义事务(指service里面的方法)传播属性 -->

<props>

<prop key="insert*">PROPAGATION_REQUIRED</prop>

<prop key="update*">PROPAGATION_REQUIRED</prop>

<prop key="save*">PROPAGATION_REQUIRED</prop>

<prop key="add*">PROPAGATION_REQUIRED</prop>

<prop key="update*">PROPAGATION_REQUIRED</prop>

<prop key="remove*">PROPAGATION_REQUIRED</prop>

<prop key="delete*">PROPAGATION_REQUIRED</prop>

<prop key="get*">PROPAGATION_REQUIRED,readOnly

</prop>

<prop key="find*">PROPAGATION_REQUIRED,readOnly

</prop>

<prop key="load*">PROPAGATION_REQUIRED,readOnly

</prop>

<prop key="change*">PROPAGATION_REQUIRED</prop>

<prop key="count*">PROPAGATION_REQUIRED</prop>

<prop key="*">PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>



<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->

<!-- 这个Processor 已经被 <context:annotation-config/> 所简化

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

-->

<context:annotation-config/>





<!-- 定义自动代理BeanNameAutoProxyCreator -->

<bean id="beanNameAutoProxyCreator"

class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->

<property name="beanNames">

<list>

<value>*Service</value>

</list>

</property>

<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器 -->

<property name="interceptorNames">

<list>

<!-- 此处可增加其他新的Interceptor -->

<value>transactionInterceptor</value>

</list>

</property>

</bean>

<bean id="AlcorTCountriesDAO" class="com.firemax.test.hibernate.AlcorTCountriesDAO">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<bean id="AlcorTProvincesDAO" class="com.firemax.test.hibernate.AlcorTProvincesDAO">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<bean id="AlcotTDistrictDAO" class="com.firemax.test.hibernate.AlcotTDistrictDAO">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<bean id="AlcorTCitysDAO" class="com.firemax.test.hibernate.AlcorTCitysDAO">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<bean id="CountryService" class="com.firemax.test.service.CountryService"/>

</beans>

注意2段标红的内容,就是这次更新的配置内容。在配置文件中使用 context
命名空间之前,必须在 <beans> 元素中声明 context
命名空间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: