aop通知前提需要的jar包
spring-aop-4.2.1.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
package com.abc.service;
public interface IsomeService {
String doSome();
}
public class SomeServiceImpl implements IsomeService {
public SomeServiceImpl() {
System.out.println("构造方法");
// TODO Auto-generated constructor stub
}
public String doSome() {
System.out.println("doSome()方法");
return "dd";
}
}
package com.abc.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
//前置通知
public class MyMethodBeforAdvice implements MethodBeforeAdvice {
//method:目标方法
//args:目标方法canlie
//target:目标对象
//该befor()方法在执行之前先调用
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("我先出来");
}
}
package com.abc.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
//后置通知 :改变其方法,改变不了其值
public class MyAferReturnAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置方法");
if(returnValue !=null){
returnValue =((String) returnValue).toUpperCase();
}
System.out.println("retunValue="+returnValue);
}
}
package com.abc.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterrecpter implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
// TODO Auto-generated method stub
System.out.println("环绕通知");
Object result = mi.proceed();//目标对象的目标方法
if(result!=null){
result = ((String) result).toUpperCase();
} 环绕通知可以改变其值
return result;
}
}
环境配置 注册目标对象,切面(通知),代理对象。 代理对象中红色标记得注意,代理对象中注册切面与目标对象,让他们联系
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注册目标通知 -->
<bean id="someService" class="com.abc.service.SomeServiceImpl" />
<!-- 注册切面,通知 -->
<bean id="myAdvice" class="com.abc.aop.MyMethodBeforAdvice" />
<!-- 代理对象 -->
<bean id="someServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="someService" />
<property name="InterceptorNames" value="myAdvice" />
</bean>
<!-- 注册切面,通知 -->
<bean id="myAdvice2" class="com.abc.aop.MyAferReturnAdvice" />
<!-- 代理对象 -->
<bean id="someServiceProxy2" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="someService" />
<property name="InterceptorNames" value="myAdvice2" />
</bean>
<!-- 注册切面,通知 -->
<bean id="myAdvice3" class="com.abc.aop.MyMethodInterrecpter" />
<!-- 代理对象 -->
<bean id="someServiceProxy3" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="someService" />
<property name="InterceptorNames" value="myAdvice3" />
</bean>
</beans>