让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>