AOP中关键性概念
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):被通知(被代理)的对象
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
AOP
即面向切面编程
2. AOP带来的好处
让我们可以 “专心做事”
案例:
public void doSameBusiness (long lParam,String sParam){
// 记录日志
log.info("调用 doSameBusiness方法,参数是:"+lParam);
// 输入合法性验证
if (lParam<=0){
throws new IllegalArgumentException("xx应该大于0");
}
if (sParam==null || sParam.trim().equals("")){
throws new IllegalArgumentException("xx不能为空");
}
// 异常处理
try{ ...
}catch(...){
}catch(...){
}
// 事务控制
tx.commit();
}
通知案例示例:
相关代码:
bookBizimpl:
package com.aop.biz.impl;
import com.aop.biz.IBookBiz;
import com.aop.ex.PriceException;
public class BookBizImpl implements IBookBiz {
public BookBizImpl() {
super();
}
public boolean buy(String userName, String bookName, Double price) {
// 通过控制台的输出方式模拟购书
if (null == price || price <= 0) {
throw new PriceException("book price exception");
}
System.out.println(userName + " buy " + bookName + ", spend " + price);
return true;
}
public void comment(String userName, String comments) {
// 通过控制台的输出方式模拟发表书评
System.out.println(userName + " say:" + comments);
}
}
PriceException:
package com.aop.ex;
public class PriceException extends RuntimeException {
public PriceException() {
super();
}
public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public PriceException(String message, Throwable cause) {
super(message, cause);
}
public PriceException(String message) {
super(message);
}
public PriceException(Throwable cause) {
super(cause);
}
}
测试:
package com.aop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aop.biz.IBookBiz;
import com.ioc.web.OrderAction;
import com.ioc.web.UserAction;
public class AopTest {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext("/spring-context.xml");
IBookBiz bean = (IBookBiz) springContext.getBean("bookProxy");
System.out.println(bean.getClass());
boolean buy = bean.buy("云殇", "星野传说", 440d);
bean.comment("云殇", "shua ");
}
}
各类通知:
前置通知
实现org.springframework.aop.MethodBeforeAdvice接口
package com.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 买书、评论前加系统日志
*
* @author xyls
*
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
String clzName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
System.out.println("买书、评论前加系统日志" + clzName + ". " + methodName + "." + params);
}
}
结果:
后置通知:
package com.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterReturningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
String clzName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
System.out.println("买书返利" + clzName + ". " + methodName + "." + params+returnValue);
}
}

环绕通知:
package com.aop.advice;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String clzName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
String params = Arrays.toString(invocation.getArguments());
Object returnValue = invocation.proceed();
System.out.println("环绕通知" + clzName + ". " + methodName + "." + params+returnValue);
return returnValue;
}
}

异常通知:
package com.aop.advice;
import org.springframework.aop.ThrowsAdvice;
/**
* 异常通知
*
* @author Administrator
*
*/
import com.aop.ex.PriceException;
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(PriceException ex) {
System.out.println("价格输入有误,购买失败,请重新输入!!!");
}
}

过滤通知(适配器,不需要单独建一个类)

spring-context.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.or g/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx ">
<bean class="com.ioc.biz.Impl.UserBizImpl2" id="userBiz"></bean>
<!-- 目标对象 -->
<bean id="bookBiz" class="com.aop.biz.impl.BookBizImpl"></bean>
<!-- 通知 -->
<bean id="myBefore" class="com.aop.advice.MyMethodBeforeAdvice"></bean>
<bean id="myAfter" class="com.aop.advice.MyAfterReturningAdvice"></bean>
<bean id="myInterceptor" class="com.aop.advice.MyMethodInterceptor"></bean>
<bean id="myThrowAdvice" class="com.aop.advice.MyThrowsAdvice"></bean>
<bean id="myAfter2"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="myAfter"></property>
<property name="pattern" value=".*buy"></property>
</bean>
<!-- -->
<bean id="bookProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="bookBiz"></property>
<property name="proxyInterfaces">
<list>
<value>com.aop.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myBefore</value>
<value>myAfter</value>
<value>myAfter2</value>
<value>myInterceptor</value>
<value>myThrowAdvice</value>
</list>
</property>
</bean>
</beans>
原文:https://www.cnblogs.com/omji0030/p/11355502.html