讓spring創建的bean
package com.b.entity;
import org.springframework.stereotype.Controller;
publicclass TestAop {
publicvoid afterF(){
System.out.println("--------之前--處理方法-----------");
}
publicvoid beforeF(){
System.out.println("--------之後--處理方法-----------");
}
}
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- spring管理hibernate -->
<!-- 把數據源模板注入sessionfactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"ref="sessionFactory"/>
</bean>
<!-- spring管理事務 -->
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 定義advice,配置傳播特性、事務隔離級別、只讀事務、回滾策略 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- get*, load*, list*, find* 不啓用回滾,只讀事務保持多條sql數據一致-->
<tx:method name="get*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>
<tx:method name="add*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>
<tx:method name="*" propagation="REQUIRED"read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 定義那些類使用事務 -->
<aop:config>
<aop:advisor pointcut="execution( * com.bd.service..*.*(..))"advice-ref="txAdvice"/>
</aop:config>
<!-- 配置事務結束 -->
<!-- 掃描組件 -->
<context:component-scan base-package="com.bd.service" />
<context:component-scan base-package="com.bd.dao" />
<!-- 配置一個bean 這個bean讓spring創建 這個bean有點類似於攔截器的類-->
<bean id="navFk" class="com.bd.entity.TestAop"/>
<aop:config>
<!-- id在下面會引用 expression爲攔截方式 -->
<aop:pointcut id="pointcut" expression="execution( *com.bd.service..*.add(..))" />
<!-- 引用剛纔spring配置的bean -->
<aop:aspect ref="navFk">
<!-- 調用 什麼方法之前調用什麼之後調用什麼 攔截方式引用上面的 pointcut的 id-->
<aop:before method="beforeF"pointcut-ref="pointcut"/>
<aop:after method="afterF" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>