一、配置文件
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p">
<!--第一种: Advisor切面-->
<!--1.被代理目标对象-->
<bean id="greetTarget" class="test.spring.proxyServices.Impl.GreetServiceImpl"/>
<!--2.定义通知-->
<bean id="greetAdvice" class="test.spring.proxy.GreetServiceBeforeAdvice"/>
<!--3.定义代理Bean-->
<bean id="greetService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--target : 代理的目标对象-->
<property name="target" ref="greetTarget"/>
<!--proxyInterfaces : 代理要实现的接口。如果多个接口可以使用以下格式赋值:-->
<!--<property name="interfaces">-->
<!--<list>-->
<!--<value>test.spring.proxyServices.GreetService</value>-->
<!--</list>-->
<!--</property>-->
<property name="proxyInterfaces" value="test.spring.proxyServices.GreetService"/>
<!--针对接口+实现类:使用JDK动态代理,如果不是针对接口代理,设置参数proxyTargetClass : 是否对类代理而不是接口,设置为true时,使用CGLib代理-->
<property name="proxyTargetClass" value="true"></property>
<!--interceptorNames : 需要织入目标的Advice singleton : 返回代理是否为单实例,默认为单例-->
<property name="interceptorNames" value="greetAdvice"/>
<!--optimize : 当设置为true时,强制使用CGLib-->
<!--<property name="optimize" value="true"/>-->
</bean>
</beans>
二、定义接口
package test.spring.proxyServices;
public interface GreetService {
public String greet(String name);
public String greet1();
}
三、实现类
package test.spring.proxyServices.Impl;
import test.spring.proxyServices.GreetService;
public class GreetServiceImpl implements GreetService {
public String greet(String name) {
System.out.println("Helle,"+name);
return name;
}
public String greet1() {
System.out.println("hello,Spring");
return "spring";
}
}
四、spring aop
package test.spring.proxy;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
/*通知类*/
public class GreetServiceBeforeAdvice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor,ThrowsAdvice {
/*前置通知*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(method);
System.out.println(args);
System.out.println(target);
System.out.println("方法执行之前执行的内容。。。");
}
/*后置通知*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method);
System.out.println(args);
System.out.println(target);
System.out.println("方法执行之后执行的内容。。。");
}
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println(invocation);
System.out.println("方法执行之前");
invocation.proceed();
System.out.println("方法执行之后");
return null;
}
public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
System.out.println("系统发生异常。。");
System.out.println(ex.getMessage());
}
}
五、测试
package test.spring.Controller.proxy;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.proxy.GreetServiceBeforeAdvice;
import test.spring.proxyServices.GreetService;
import test.spring.proxyServices.Impl.GreetServiceImpl;
public class SpringAopAction {
@Test
public void test(){
//普通实现类
GreetService greetService = new GreetServiceImpl();
//spring aop
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
GreetService greetService = (GreetService) applicationContext.getBean("greetTarget");
greetService.greet("wyj");
}
}