AOP中关键性概念
AOP即面向切面编程
-
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
-
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑 -
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程 -
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的 -
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序) -
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
案例
**IBookBiz **
package com.aop.biz;
public interface IBookBiz {
// 购书
public boolean buy(String userName, String bookName, Double price);
// 发表书评
public void comment(String userName, String comments);
}
BookBizImpl
package com.aop.biz.impl;
import com.aop.biz.IBookBiz;
import com.aop.biz.exception.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.biz.exception;
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);
}
}
前置通知
在连接点之前执行的通知
MyMethodBeforeAdvice
package com.aop.biz.advice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Arrays;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
String methodName = method.getName();
String paras = Arrays.toString(objects);
String clazName = o.getClass().getName();
System.out.println("[前置通知] : 方法名" + methodName + "参数" + paras + "类名" + clazName );
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byType"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- spring的aop -->
<!-- 目标 -->
<bean id="bookBiz" class="com.aop.biz.impl.BookBizImpl"></bean>
<!-- 通知 -->
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.aop.biz.advice.MyMethodBeforeAdvice"></bean>
<!-- 组合器 -->
<bean id="bookBizProxy" 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>
<!-- 前置通知的id -->
<value>myMethodBeforeAdvice</value>
</list>
</property>
</bean>
</beans>
后置通知
在连接点正常完成后执行的通知
package com.aop.biz.advice;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
import java.util.Arrays;
public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
String methodName = method.getName();
String paras = Arrays.toString(objects);
String clazName = o1.getClass().getName();
System.out.println("[后置通知] : 方法名" + methodName + "参数" + paras + "类名" + clazName + "返回值" + o);
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byType"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- spring的aop -->
<!-- 目标 -->
<bean id="bookBiz" class="com.aop.biz.impl.BookBizImpl"></bean>
<!-- 通知 -->
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.aop.biz.advice.MyMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean id="myAfterReturningAdvice" class="com.aop.biz.advice.MyAfterReturningAdvice"></bean>
<!-- 组合器 -->
<bean id="bookBizProxy" 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>myMethodBeforeAdvice</value>
<!-- 后置通知 -->
<value>myAfterReturningAdvice</value>
</list>
</property>
</bean>
</beans>
package com.aop.biz.advice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aop.biz.IBookBiz;
public class demo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
//IBookBiz bean = (IBookBiz) context.getBean("bookBiz");//调用的是BookBizImpl的实现类
IBookBiz bean = (IBookBiz) context.getBean("bookBizProxy");
bean.buy("李现", "亲爱的,热爱的", 66d);
bean.comment("红红", "爱上了");
}
}
环绕通知:
包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
它通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用,这样目标方法就不会执行)
package com.aop.biz.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
String methodName = arg0.getMethod().getName();
String clssName = arg0.getThis().getClass().getName();
String param = Arrays.toString(arg0.getArguments());
System.out.println("【环绕前通知】:方法名为:"+methodName+",类为:"+clssName+",参数有:"+param);
//returnValue是目标对象方法的返回值
//在arg0.proceed之前是环绕前,之后就是环绕后
Object returnValue = arg0.proceed();
System.out.println("【环绕后通知】:方法名为:"+methodName+", 这个方法的返回值:"+returnValue+",类为:"+clssName+",参数有:"+param);
return returnValue;
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byType"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- spring的aop -->
<!-- 目标 -->
<bean id="bookBiz" class="com.aop.biz.impl.BookBizImpl"></bean>
<!-- 通知 -->
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.aop.biz.advice.MyMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean id="myAfterReturningAdvice" class="com.aop.biz.advice.MyAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean id="myMethodInterceptor" class="com.aop.biz.advice.MyMethodInterceptor"></bean>
<!-- 组合器 -->
<bean id="bookBizProxy" 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>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value>
<value>myMethodInterceptor</value><!-- 环绕通知 -->
</list>
</property>
</bean>
</beans>
异常通知
出现异常执行系统提示,然后进行处理
public class MyThrowsAdvice implements ThrowsAdvice {
//方法抛出Exception 错误就会走这个方法
public void afterThrowing(Exception ex){
System.out.println("错误!!!请重新输入...");
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byType"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- spring的aop -->
<!-- 目标 -->
<bean id="bookBiz" class="com.aop.biz.impl.BookBizImpl"></bean>
<!-- 通知 -->
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.aop.biz.advice.MyMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean id="myAfterReturningAdvice" class="com.aop.biz.advice.MyAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean id="myMethodInterceptor" class="com.aop.biz.advice.MyMethodInterceptor"></bean>
<!-- 异常通知 -->
<bean id="myThrowsAdvice" class="com.aop.biz.advice.MyThrowsAdvice"></bean>
<!-- 组合器 -->
<bean id="bookBizProxy" 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>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value> <!-- 异常通知-->
</list>
</property>
</bean>
</beans>
过滤通知(适配器)
适配器=通知(Advice)+切入点(Pointcut)
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
default-autowire="byType"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- spring的aop -->
<!-- 目标 -->
<bean id="bookBiz" class="com.aop.biz.impl.BookBizImpl"></bean>
<!-- 通知 -->
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.aop.biz.advice.MyMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean id="myAfterReturningAdvice" class="com.aop.biz.advice.MyAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean id="myMethodInterceptor" class="com.aop.biz.advice.MyMethodInterceptor"></bean>
<!-- 异常通知 -->
<bean id="myThrowsAdvice" class="com.aop.biz.advice.MyThrowsAdvice"></bean>
<!-- 过滤通知 -->
<bean id="myRegexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="myAfterReturningAdvice" ></property>
<property name="pattern" value=".*buy"></property>
</bean>
<!-- 组合器 -->
<bean id="bookBizProxy" 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>myMethodBeforeAdvice</value>
<!-- <value>myAfterReturningAdvice</value> -->
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
<!-- 使用这个后置通知,而不用之前的-->
<value>myRegexpMethodPointcutAdvisor</value>
</list>
</property>
</bean>
</beans>