AOP
为了帮助理解先将一下动态代理实现切面编程。
import com.v5.AspectSpring.bean.Person;
//proxyInterface 只是一个标志接口 什么东西都没有 你也可以不用
public class ProxyImpl implements ProxyInterface{
private Person per;
public ProxyImpl(Person per){
this.per=per;
}
public Person getProxy() {
// TODO Auto-generated method stub
Person proxy=null;
//生成代理的一个参数
/*个人理解当使用代理对象调用方法时其实方法体中的 类容就是invoke中的类容代理只不过生成了一个和被代理对象一样的方法名。真实调用方法的功能是在 method.invoke(per, args);
1.第一个参数是只现在正在被代理的对象的 代理对象 (有点拗口 其实就是我们这个方法返回的代理对象)一般用不着
2.通过反射获取被代理对象的方法的对象(简单理解为正在被调用的被代理对象的方法对象)
3.被动调用方法的参数
*/
InvocationHandler handler=new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("before "+method.getName()+args+" code here");
Object returns=method.invoke(per, args);
System.out.println("after"+returns);
return returns;
}
};
proxy=(Person) Proxy.newProxyInstance(per.getClass().getClassLoader(), new Class[]{Person.class}, handler);
/*
三个参数分别有什么作用
1.加载代理对象的类加载器
2.代理对象的类型其实就是为了重新生成一个 有代理类容的被代理对象
3.代理要做哪些事情
*/
return proxy;
}
}
举个例子:
package com.v5.AspectSpring.bean;
public class Student implements Person {
@Override
public String readBook() {
// TODO Auto-generated method stub
System.out.println("reading......");
return "the think after reading what you had read";
}
}
代理以后
通过 给的接口Class 对象不难在创建一个实现类
package com.v5.AspectSpring.bean;
public class StudentProxy implements Person {
@Override
public String readBook() {
// TODO Auto-generated method stub
String result=(String)handler.invoke();
return result;
}
}
aop 概念
aop道理是一样的,面向切面 就是把代码分层把相同的属性代码集中到一起,比如业务逻辑层中我们只写业务逻辑。要加其他东西的话可以你在其他的层次中加,最后连接过来就行了。这样代码清晰不肿胀。
Aop各种通知
-先别急着上手我们首先要导入使用到的jar包 :aopaliance .jar aspectj.weaver.jar aspects.jar
前置通知&后置通知&返回通知&异常通知&环绕通知
首先创建一个person的实现类 相当与被代理的类
package com.v5.AspectSp.bean;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
public class Student implements Person {
@Override
public String readBook() {
// TODO Auto-generated method stub
System.out.println("reading......");
return "the think after reading what you had read";
}
}
在创建一个切面(用@Aspect声明),由于这个就相当于代理要做的处理类了
package com.v5.AspectSp.asp;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
//声明 底层会找到有这个注解的类 来生成代理对象
@Aspect
@Component
public class AspectDoing {
//前置通知 这个写的是详细的 后置通知是用通配符。
//execution 理解为执行 后面是 一个具体的方法 加上全类名()里面是类型如果是 例:(int)
@Before("execution(public String com.v5.AspectSp.bean.Person.readBook())")
private void afterMethod(){
System.out.println("before method");
}
//后置通知 类比一下前置通知。
//这里方法为什么可以加参数也可不加呢。
//参数里面有被通知方法的信息
@After("execution(* com.v5.AspectSp.bean.*.*(..))")
private void beforeRun(JoinPoint jp) {
// TODO Auto-generated method stub
String methodname=jp.getSignature().getName();
List list=Arrays.asList(jp.getArgs());
System.out.println("after "+methodname+" "+list );
}
//返回通知 获取返回值 存在 returning=“”
@AfterReturning(value="execution(* com.v5.AspectSp.bean.*.*(..))",returning="result")
private void afterRetrunning(Object result) {
// TODO Auto-generated method stub
//result=15;
System.out.println(result);
}
//异常通知 throwing 会获取异常
@AfterThrowing(value="execution(* com.v5.AspectSp.bean.*.*(..))",throwing="ex")
public void catchEx(Exception ex){
System.out.println(ex);
}
//环绕通知
//就是一个代理。只不过method 变成了pjd
@Around("execution(* com.v5.AspectSp.bean.*.*(..))")
private Object aroundMethd(ProceedingJoinPoint pjd) {
// TODO Auto-generated method stub
Object result =null;
String methodName=pjd.getSignature().getName();
try{
//前置通知
System.out.println("before");
result=pjd.proceed();
//返回通知
System.out.println("returnning "+result);
}catch(Throwable e){
e.printStackTrace();
//异常通知
System.out.println(e);
}
//后置通知
System.out.println("after");
return result;
}
}
spring 配置文件怎么能少 注意要 导入命名空间 aop 和context
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- context 由于是通过注解来生成bean实例所以少不了扫描 -->
<context:component-scan base-package="com.v5.AspectSp"></context:component-scan>
<!-- aspect自动生成代理见名知意 也是编程的一个要求哦-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
注:即使被通知的方法抛出异常也会执行哦!
就相当与通知在try catch块外面一样。
小知识点
1.如果有多个 aspect 切面 要怎么处理呢 。可以在
上面加上一个order 数字越小越优先
@Order(1)
@Aspect
@Component
2.重用切点表达式
@Pointcut("execution(* com.v5.AspectSp.bean.*.*(..))")
public void pointcut(){};
同一个类下
@Around("pointcut()")
同一个包下
@Around("切面类.pointcut()")
不同的包下
@Around("全类名.pointcut()")
3.通过配置文件来配置AOP
<aop:config>
<aop:poingcut expression="execution(* com.v5.AspectSp.bean.*.*(..))") id="pointcut"/>
<aop:aspect ref="Aspect" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:config>