package com.gqc.aop03;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
//环绕通知可以修改目标方法的返回结果
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("执行环绕通知:目标方法执行之前");
//执行目标方法
Object result = invocation.proceed();
System.out.println("执行环绕通知:目标方法执行之后");
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.gqc.aop03.SomeServiceImpl"/>
<!-- 注册切面:后置通知 -->
<bean id="myAdvice" class="com.gqc.aop03.MyMethodInterceptor"/>
<!-- 生成代理对象 -->
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 指定目标对象 -->
<property name="target" ref="someService"/>
<!-- <property name="targetName" value="someService"/> -->
<!-- 指定切面 -->
<property name="interceptorNames" value="myAdvice"/>
</bean>
</beans>