一 、Before通知
然后在applicationContex.xml中配置如下
获取目标对象时用下面的方法
输出结果
二、AfterReturning 通知
使用AfterReturning 通知需要实现AfterReturningAdvice接口
然后在applicationContex.xml 中添加bean就行了
三、Throw通知
使用Throw通知 需要实现ThrowsAdvice接口
并且提供afterThrowing方法 , 该方法定义如下:
然后在applicationContex.xml 中添加相应的配置就行了
四、Intereptor通知
需要继承DelegatingIntroductionInterceptor类
重写invok方法
然后在applicationContex.xml中添加相应的配置就行了。
使用Spring中的Before通知需要实现 MethodBeforeAdvice 接口
public class DaoBeforeAdvice implements MethodBeforeAdvice{
/**
* @param target 代理目标类
* @param method 代理目标类的的方法
* @param args 代理目标类的的方法的参数
* 如果需要对被代理的目标类操作 可以将target强转成目标类
*/
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println((target instanceof PersonDao) + "\t" + method.getName() + "\t" + args.length);
}
然后在applicationContex.xml中配置如下
<bean id="daoBefore" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="personDao"></property>
<property name="interceptorNames">
<array>
<value>beforeDao</value>
</array>
</property>
</bean>
<bean name = "personDao" class="com.zf.dao.PersonDao" ></bean>
<bean name="beforeDao" class="com.zf.aspect.DaoBeforeAdvice"></bean>
获取目标对象时用下面的方法
ApplicationContext contex = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao personDao = (PersonDao)contex.getBean("daoBefore");
personDao.savePerson();
输出结果
true savePerson 1
savePerson()...
二、AfterReturning 通知
使用AfterReturning 通知需要实现AfterReturningAdvice接口
public class DaoAfter implements AfterReturningAdvice{
/**
* @param returnValue 返回对象
*/
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("after...");
}
然后在applicationContex.xml 中添加bean就行了
<bean name = "daoAfter" class="com.zf.aspect.DaoAfter"></bean>
<bean id="daoBefore" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="personDao"></property>
<property name="interceptorNames">
<array>
<value>beforeDao</value>
<value>daoAfter</value> <-- 这里也要加上 -->
</array>
</property>
</bean>
三、Throw通知
使用Throw通知 需要实现ThrowsAdvice接口
并且提供afterThrowing方法 , 该方法定义如下:
//[Method], [args], [target], 该方法还可拥有这些参数 ,不过是可选的
public void afterThrowing(Exception subclassOfThrowable){
System.out.println(subclassOfThrowable.getMessage());
}
然后在applicationContex.xml 中添加相应的配置就行了
四、Intereptor通知
需要继承DelegatingIntroductionInterceptor类
重写invok方法
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println("intereptor...");
if(true) //这里可以添加一些业务逻辑的判断 , 是否执行invoke方法
return null;
return super.invoke(mi);
}
然后在applicationContex.xml中添加相应的配置就行了。
代理事务
<bean id="petStore"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref local="targetService"/></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>