1、创建增强类型
spring的增强类型为5个
1、前置,后置,环绕,异常,引介,这些增强接口都有一些方法,
在接口方法中定义横切逻辑,就可以将它们织入到目标类方法的相应连接点的位置。
我们这里只看前置和环绕。
我们先来温故一下,spring如何使用jdk动态代理实现aop
1、把业务逻辑里的横切逻辑拿出来只留下业务逻辑
2、创建横切逻辑类,实现InvocationHandler接口,并给构造方法传入一个目标类对象Object target
3、实现接口里的方法invoke(Object proxy,Method method,Object[] args),
在invoke方法中调用编写横切逻辑,然后调用method.invoke(target,args)采用反射机制调用业务类方法,即目标类对象方法。
4、测试类,声明一个业务类接口target,并业务类实现类实例化它;声明一个增强类(织入器)handler并实例化(织入器就是整合和业务逻辑和横切逻辑);
在声明一个业务类接口proxy,并调用Proxy的静态方法newProxyInstance实例化一个整合后的代理对象。该方法有三个参数,第一个是目标类的类加载器,第二个
是创建代理实例所实现的接口,第三个是织入器。
这里没有用到spring配置文件,完全硬编码。
硬编码就是把代码写死,如果要改功能就只能修改代码,软编码就是不需要修改代码,只需要修改配置就可以修改功能。
spring底层就是采用动态代理实现aop的
怎么使用增强类型实现aop
spring使用前置增强:
- 创建一个前置增强类并且实现MethodBeforeAdvice接口,实现一个方法before(Method method,Object[] args,Object target) throws Throwable
method为目标类方法,args为目标类方法的入参,target为目标类实例。然后直接在before()方法里写横切逻辑代码,不再需要调用method.invoke,因为这里已经是前置通知了。
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/*
* 前置增强类
*/
public class LogBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("我是前置日志.......");
}
}
- 采用硬编码的方式测试前置通知,声明一个业务逻辑接口target,业务逻辑实现类实例化它;声明一个前置增强类advice并实例化它;声明一个
spring提供的代理工厂ProxyFactory,pf并实例化它;然后设置代理目标pf.setTarget(target),为代理目标添加增强pf.addAdvice(advice);然后声明一个业务服务接口,并用pf.getProxy()实例化它。解析PrxoyFactory以后再说。
/*
采用硬编码方式测试前置通知
*/
ForumService target = new ForumServiceImpl();
LogBeforeAdvice advice = new LogBeforeAdvice();
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvice(advice);
ForumService proxy = (ForumService) pf.getProxy();
proxy.removeForum(1);
- 采用spring配置方式
1、声明增强类bean,advice
2、声明业务逻辑实现类bean,target
3、声明ProxyFactoryBean,pf
并且传参,proxyInterfaces指定代理接口一般只有业务逻辑接口,如果是多个接口,使用<list>元素;interceptorNames指定使用的增强bean,target;target-ref指定对哪个bean进行代理即对哪个业务实现类bean进行代理,这里是target。
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="advice" class="com.ljs.springaop.advice.LogBeforeAdvice"/>
<bean id="target" class="com.ljs.springaop.advice.ForumServiceImpl"/>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target"/>
<property name="interceptorNames">
<value>advice</value>
</property>
<property name="proxyInterfaces" value="com.ljs.springaop.advice.ForumService"/>
</bean>
</beans>
/*
采用软编码方式测试前置通知
*/
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/ljs/springaop/advice/spring.xml");
ForumService proxy2 = (ForumService) context.getBean("proxy");
proxy2.removeForum(1);
spring使用环绕增强:
- 创建一个环绕增强类并且实现MethodInterceptor,这个有点像spring使用jdk动态代理实现aop,环绕综合实现了前置和后置两者的功能。然后实现该方法,invoke(MethodInvocation invocation) throws Throwable,注意这个接口是org.aopalliance.intercept.MethodInterceptor,别搞错了。
- 再该方法中需要先获取该目标方法的参数,用Object[] args=invocation.getArguments()然后再传值String clientName = (String)args[0]
- 在编写横切逻辑
- 通过反射机制调用目标方法Object obj = invocation.proceed();
- 编写横切逻辑
- return obj;
- 采用spring配置方式,增强bean,业务逻辑实现类bean,spring代理工厂bean,然后跟前置一样传参。
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class LogInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] args = invocation.getArguments();//目标方法入参
int number = (int) args[0];
System.out.println("我是环绕log1,你想删除的是"+number);//在目标方法前执行调用
Object obj = invocation.proceed();//通过反射机制调用目标方法
System.out.println("我是环绕log2");//在目标方法后执行调用
return obj;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="advice" class="com.ljs.springaop.advice.interceptor.LogInterceptor"/>
<bean id="target" class="com.ljs.springaop.advice.interceptor.ForumServiceImpl"/>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target"/>
<property name="interceptorNames">
<value>advice</value>
</property>
<property name="proxyInterfaces" value="com.ljs.springaop.advice.interceptor.ForumService"/>
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/ljs/springaop/advice/interceptor/spring.xml");
ForumService proxy = (ForumService) context.getBean("proxy");
proxy.removeForum(1);
}