zoukankan      html  css  js  c++  java
  • 注解的力量 Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置

     在(三)里面。我们引入了 <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文件
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.     xmlns:context="http://www.springframework.org/schema/context"
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5.     xmlns:tx="http://www.springframework.org/schema/tx"
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    7.                                             http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd
    8.                                             http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    9.     >
    10.     <bean id="entityManagerFactory"
    11.         class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    12.         <property name="persistenceUnitName" value="testerPU" />
    13.     </bean>
    14.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    15.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    16.     </bean>
    17.     <tx:annotation-driven transaction-manager="transactionManager" />
    18.     <bean id="transactionInterceptor"
    19.         class="org.springframework.transaction.interceptor.TransactionInterceptor">
    20.         <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
    21.         <property name="transactionManager">
    22.             <ref local="transactionManager" />
    23.         </property>
    24.         <property name="transactionAttributes">
    25.             <!-- 下面定义事务(指service里面的方法)传播属性 -->
    26.             <props>
    27.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
    28.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
    29.                 <prop key="save*">PROPAGATION_REQUIRED</prop>
    30.                 <prop key="add*">PROPAGATION_REQUIRED</prop>
    31.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
    32.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>
    33.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>
    34.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly
    35.                 </prop>
    36.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly
    37.                 </prop>
    38.                 <prop key="load*">PROPAGATION_REQUIRED,readOnly
    39.                 </prop>
    40.                 <prop key="change*">PROPAGATION_REQUIRED</prop>
    41.                 <prop key="count*">PROPAGATION_REQUIRED</prop>
    42.                 <prop key="*">PROPAGATION_REQUIRED</prop>
    43.             </props>
    44.         </property>
    45.     </bean>
    46.     
    47.     <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
    48.     <!--  这个Processor 已经被 <context:annotation-config/> 所简化   
    49.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    50.     -->
    51.     <context:annotation-config/>
    52.     
    53.     
    54.     <!-- 定义自动代理BeanNameAutoProxyCreator -->
    55.     <bean id="beanNameAutoProxyCreator"
    56.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    57.         <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
    58.         <property name="beanNames">
    59.             <list>
    60.                 <value>*Service</value>
    61.             </list>
    62.         </property>
    63.         <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器  -->
    64.         <property name="interceptorNames">
    65.             <list>
    66.                 <!-- 此处可增加其他新的Interceptor -->
    67.                 <value>transactionInterceptor</value>
    68.             </list>
    69.         </property>
    70.     </bean>
    71.     <bean id="AlcorTCountriesDAO" class="com.firemax.test.hibernate.AlcorTCountriesDAO">
    72.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    73.     </bean>
    74.     <bean id="AlcorTProvincesDAO" class="com.firemax.test.hibernate.AlcorTProvincesDAO">
    75.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    76.     </bean>
    77.     <bean id="AlcotTDistrictDAO" class="com.firemax.test.hibernate.AlcotTDistrictDAO">
    78.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    79.     </bean>
    80.     <bean id="AlcorTCitysDAO" class="com.firemax.test.hibernate.AlcorTCitysDAO">
    81.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    82.     </bean>
    83.     <bean id="CountryService" class="com.firemax.test.service.CountryService"/>
    84. </beans>

    注意2段标红的内容,就是这次更新的配置内容。在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。
  • 相关阅读:
    JSP九大内置对象的作用和用法总结(转)
    Java web的几种异常处理 (转)
    response.getWriter().write()与out.print()的区别(转)
    【JavaWeb】Session(转)
    java web中cookies的用法 转
    1123
    1120
    jsp 内置对象
    include与jsp:include区别
    11.24作业1
  • 原文地址:https://www.cnblogs.com/winkey4986/p/2354543.html
Copyright © 2011-2022 走看看