AOP联盟通知类型
AOP联盟为通知Advice定义了org.aopalliance.aop.Advice
开发通知类:
Spring按照通知Advice在目标类方法的连接点位置,常用:
前置通知 org.springframework.aop.MethodBeforeAdvice:在目标方法执行前实施增强,可以拦截目标方法的执行
后置通知 org.springframework.aop.AfterReturningAdvice:在目标方法执行后实施增强 ,可以获取到方法的返回值
环绕通知 org.aopalliance.intercept.MethodInterceptor:在目标方法(切入点)执行前后实施增强
invocation.proceed();//必须手动执行目标方法
使用AOP联盟来编写通知类
通知概念:
在目标方法之前加入通知:log: 这个通知就叫做前置通知
在目标方法之后加入通知:log: 这个通知就叫做后置通知
环绕通知:包含(前置+后置)
开发流程:
第一步:创建通知类
我们在这里创建了很多通知类 前置 后置 以及环绕
前置 :
@Component("beforeLog")
public class LogBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
Log.info("我是打印输出通知 前置方法");
}
}
后置:
@Component("afterLog")
public class LogAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
Log.info("我是打印输出通知 后置方法");
}
}
环绕:
@Component("circleLog")
public class LogCircleAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Log.info("环绕前");
Object proceed = invocation.proceed();
Log.info("环绕后" );
return proceed;
}
}
带返回值的环绕:
@Component("returnInfoCircleLog")
public class ReturnInfoCircleAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Log.info("环绕前");
String s = (String) invocation.proceed();
Log.info("环绕后" + s);
return s;
}
}
application 配置文件如下所示:
<bean id="studentServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="studentService"></property>
<property name="interceptorNames">
<list>
<value>circleLog</value>
</list>
</property>
<property name="proxyInterfaces">
<array>
<value>com.zyh.service.StudentService</value>
</array>
</property>
</bean>
测试方法如下所示:
@Test
public void testStudentService(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) classPathXmlApplicationContext.getBean("studentServiceProxy");
studentService.addStudent();
}
结果如下所示:

当我们想要测试带返回值得环绕的时候 我们修改代码如下所示:
配置文件如下所示:
<bean id="returnInfoServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="studentService"></property>
<property name="interceptorNames">
<list>
<value>returnInfoCircleLog</value>
</list>
</property>
<property name="proxyInterfaces">
<array>
<value>com.zyh.service.StudentService</value>
</array>
</property>
</bean>
修改实现类如下所示:

测试方法如下所示 :
@Test
public void testStudentService(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService1 = (StudentService) classPathXmlApplicationContext.getBean("returnInfoServiceProxy");
studentService1.printInfo();
}
输出结果如下所示:

ProxyFactoryBean
这个代理类返回的对象是谁?
是通过底层getObject()返回具体被代理的目标对象。不是自己工厂的对象。
在代理类,AOP编程有3小步:
1)整合目标类
2)整合通知类
3)关联接口
三大步三小步
网友评论