Spring常用的配置

来源:互联网 发布:js购物车功能的实现 编辑:程序博客网 时间:2024/05/01 06:03

<?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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config/>  <!-- 打开注解配置 -->
    <context:component-scan base-package="com.tr.itcase"/>      <!-- 扫面类 -->
 <context:property-placeholder location="classpath:jdbc.properties" />  <!-- 读取文件配置 -->
 <aop:aspectj-autoproxy />                                           <!-- 打开切面配置 -->
 <bean id="springProxy" class="com.tr.itcase.aop.SpringProxyXML" />       
 <bean id="personImpl" class="com.tr.itcase.impl.PersonImpl">
  <property name="dataSource" ref="dataSource" />               <!-- 为属性注入 -->
 </bean>
 <aop:config>
  <aop:aspect id="asp" ref="springProxy">                         <!-- 定义切面类 -->
   <aop:pointcut expression="execution (* com.tr.itcase.impl..*.*(..))"       
    id="myCut" />                                               <!-- 定义切入点 -->
   <!--
    <aop:pointcut expression="execution (* com.tr.itcase.impl..*.*(..))"
    id="myCut" /> 拦截包下的所有的方法 <aop:pointcut expression="execution (*
    com.tr.itcase.impl.*.*(java.lang.String,..))" id="myCut" />
    拦截包下的方法第一个参数为String类型 <aop:pointcut expression="execution (!void
    com.tr.itcase.impl.*.*(..))" id="myCut" /> 拦截包下的方法返回非void类型的方法
   -->
   <aop:before method="beforeMethod" pointcut-ref="myCut" />                   <!-- 定义通知 -->
   <aop:after method="afterMethod" pointcut-ref="myCut" />  <!-- 最终通知 -->
   <aop:after-returning method="methodAfter"
    pointcut-ref="myCut" />  <!-- 后置通知 -->
   <aop:around method="doAround" pointcut-ref="myCut" />
  </aop:aspect>
 </aop:config>


 <!-- jdbc操作 -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">          <!-- 定义数据源 -->
  <property name="driverClassName" value="${driverClassName}">
  </property>
  <property name="url" value="${url}">
  </property>
  <property name="username" value="${username}" />
  <property name="password" value="${password}" />
  <property name="initialSize" value="${initialSize}" />
  <property name="maxActive" value="${maxActive}" />
  <property name="maxIdle" value="${maxIdle}" />
  <property name="minIdle" value="${minIdle}" />
 </bean>
 <bean id="txManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">         <!-- jdbc事物管理类 -->
  <property name="dataSource" ref="dataSource"></property>
 </bean>   <!-- 把dataSource注入到spring事物管理类 -->
 <tx:annotation-driven transaction-manager="txManager" />  <!-- 使用注解事物管理,把事物管理类注入 -->
 <aop:config>
      <aop:pointcut expression="exectution (* com.tr.itcase.impl.*(..))" id="txPointcut"/>   <!-- 为事物定义切面 -->
      <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>                      <!-- 把事物织入切面 -->
 </aop:config>


<!-- 使用xml定义事物的传播路径 -->
 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
    <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>   <!-- 拦截所有方法以get开头的  并且是只读的  不开启事物 -->
    <tx:method name="*"/>
  </tx:attributes>
 </tx:advice>


</beans>