springaop需要的jar包_SpringAOP使用详解

几个AOP相关的概念

  1. Aspect:在SpringAOP中,切面就是一个普通的类,然后要么加上@Aspect注解,要么在xml中配置下,它里面包含了Advice和Pointcut。
  2. Joint point:程序执行的时候的某个点,比如执行方法的时候,抛出异常的时候,在SpringAOP中一般代表方法的执行。
  3. Advice:在特定的Joint point的时候,Aspect要做的事情,一般叫通知,有 “Before advice(前置通知)” 、 “After returning(正常返回)”、“After throwing(抛出异常)”、“After (finally)(不管是否有异常)”、Around advice(环绕通知) 。
  4. Pointcut:满足条件的连接点,SpringAOP使用AspectJ的切点表达式语言来定义切点。
  5. Target object: 目标对象,被代理的对象
  6. AOP proxy:代理对象

AOP代理

Spring AOP是基于代理来实现的,默认是使用JDK的动态代理,如果target 对象不是接口,会使用Cglib进行代理。

@Aspect与SpringAOP

Spring AOP只是借鉴了AspectJ的语法,但是内部的实现还是Spring自己来实现的。

如何开启Spring AOP

(1)添加依赖,在classpath中添加aspectjweaver.jar

(2)添加配置

如果是使用java配置类:

@Configuration
//在配置类上添加@EnableAspectJAutoProxy
@EnableAspectJAutoProxy
public class AppConfig {
}

如果是使用xml:

<aop:aspectj-autoproxy/>

如何声明一个切面Aspect

java配置类方式:

@Aspect
public class NotVeryUsefulAspect {
}

xml方式:

<!--NotVeryUsefulAspect类上 必须要有@Aspect注解-->
<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect">
<!-- configure properties of the aspect here -->
</bean>

需要说明一下,切面仅仅是一个普通的类,是无法自动被Spring容器发现的,可以加上@Component加入到容器中,另外,切面本身是不可以被继续被代理的。

如何声明一个切点Pointcut

SpringAOP只支持在Spring Bean的方法执行的时候的切点,切点包含两部分内容,一个是切点签名方法,一个是切点表达式,切点签名方法就是普通的方法,但是返回类型必须是void,切点表达式是用@Pointcut来表示。比如:

@Pointcut("execution(* transfer(..))") // 切点表达式
private void anyOldTransfer() {} // 切点签名方法

切点类型

  1. execution: 用来匹配方法的执行
  2. within: 用来匹配特定类内部方法执行
  3. this: 用来匹配代理对象是特定类型(Spring AOP proxy is an instance of the given type)
  4. target: 用来匹配目标对象是特定类型(Object being proxied is an instance of the given type)target和this的区别可以参考:https://my.oschina.net/OttoWu/blog/3304147/print
  5. args: 用来匹配参数是特定类型
  6. @args: 参数上有特定注解
  7. @annotation: 用来匹配加了特定注解的方法
  8. @target: 用来匹配加了特定注解的类
  9. @within: 用来匹配加了特定注解的类

@target和@within的区别是啥???

可以用|| && ! 来串联多个表达式。

举个例子:

execution(public * *(..))任意返回值,任意方法名,任意参数,只要是public就可以
execution(* set*(..)):以set开头的方法
execution(* com.xyz.service.AccountService.*(..))AccountService的所有方法
execution(* com.xyz.service.*.*(..)) service包下面的所有类的所有方法
execution(* com.xyz.service..*.*(..)) service包以及子包下面的所有类的所有方法
within(com.xyz.service.*):service包里面
within(com.xyz.service..*):service包以及子包里面
this(com.xyz.service.AccountService):实现了AccountService的代理对象
target(com.xyz.service.AccountService):实现了AccountService的目标对象
args(java.io.Serializable):方法只有一个参数,并且实现了Serializable
args(java.io.Serializable, java.lang.String,..):方法的前两个参数是Serializable和String
@target(org.springframework.transaction.annotation.Transactional)目标对象上有Transactional注解
@within(org.springframework.transaction.annotation.Transactional)目标对象上有Transactional注解
@annotation(org.springframework.transaction.annotation.Transactional)方法上定义了Transactional注解
@args(com.xyz.security.Classified):方法只有一个参数,并且参数上有Classified注解
bean(tradeService):名字叫tradeService的bean
bean(*Service):bean的名字以Service结尾

如何声明通知Advice

(1)@Before:方法执行之前

@Aspect
public class ExecutionAspect {
@Pointcut("execution(* com.github.xjs..*.*(..) )")
public void demo(){}
@Before("demo()")
public void before(JoinPoint jp){
  }
}

也可以把@Pointcut表达式嵌入到@Before里面:

public class ExecutionAspect {
@Before("execution(* com.github.xjs..*.*(..) )")
public void before(JoinPoint jp){
  }
}

(2)@AfterReturning:方法正常结束,可以拿到返回值

@Aspect
public class AfterReturningExample {
@AfterReturning(
        pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
        returning="retVal")
public void doAccessCheck(Object retVal) {
// ...
    }
}

(3)@AfterThrowing:方法抛出异常之后,可以拿到异常

@Aspect
public class AfterThrowingExample {
@AfterThrowing(
        pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
        throwing="ex")
public void doRecoveryActions(DataAccessException ex) {
// ...
    }
}

(4)@After:不管方法是否正常还是异常结束,所以里面需要处理正常和异常两种情况

(5)@Around:从方法执行之前,一直到方法执行完成,第一个参数必须是ProceedingJoinPoint。

@Aspect
public class AroundExample {
@Around("com.xyz.myapp.SystemArchitecture.businessService()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
}

如何向Advice传递参数

可以使用args来绑定参数,只需要把args切点表达式换成参数名即可,当Advice被调用的时候,就会把参数的值传递进来,需要说明的是参数也可以是泛型类型,

举个例子:

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation() && args(account,..)")
public void validateAccount(Account account) {
// ...
}

args(account,..)这个切点表达式有2个作用,首先它限定了只匹配至少有一个参数的方法,并且第一个参数的类型必须是Account,然后,它使用account参数把Account对象传递到了Advice里面。

当然以下写法也是一样的:

@Pointcut("com.xyz.myapp.SystemArchitecture.dataAccessOperation() && args(account,..)")
private void accountDataAccessOperation(Account account) {}
@Before("accountDataAccessOperation(account)")
public void validateAccount(Account account) {
// ...
}

再看一个例子:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auditable {
AuditCode value();
}
@Before("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(auditable)")
public void audit(Auditable auditable) {
AuditCode code = auditable.value();
// ...
}

如何来确定参数的名字

java的反射是没法获取方法的参数的名字的,所以SpringAOP使用如下的策略来确定参数的名字:

(1)首先是看是否明确使用了argNames参数

(2)然后是查看class文件的本地变量表,如果编译的时候加入了-g:vars选项,就可以从本地变量表中拿到参数名

(3)再然后Spring会自己猜,比如:如果只有一个参数那肯定会猜出来,如果猜不出来就会抛异常AmbiguousBindingException

(4)抛异常IllegalArgumentException

看下argNames的使用:

@Before(value="com.xyz.lib.Pointcuts.anyPublicMethod() && target(bean) && @annotation(auditable)",
        argNames="bean,auditable")
public void audit(JoinPoint jp, Object bean, Auditable auditable) {
    AuditCode code = auditable.value();
// ... use code, bean, and jp
}

注意:JoinPoint、ProceedingJoinPoint、JoinPoint.StaticPart这些是不需要argNames的。

如何确定Advice执行的顺序

(1)如果是before,谁的优先级高谁先执行

(2)如果是after,谁的优先级高谁后执行

(3)除非明确指定顺序,否则,顺序是不确定的

可以使用@Order来设置顺序

Introductions

Introductions可以给目标对象添加新的接口方法。@DeclareParents注解就是用来给匹配的类添加新的接口。

举个例子:

public interface Vehicle {
void driving();
}
//Car是一个交通工具,可以driving。
public class Car implements Vehicle {
@Override
public void driving() {
        System.out.println("开车了");
    }
}
public interface Intelligent {
void selfDriving();
}
//IntelligentCar是添加了无人驾驶功能的车
public class IntelligentCar  implements Intelligent{
public void selfDriving(){
       System.out.println("开启无人驾驶了");
   }
}

如何让所有实现了Vehicle的普通的汽车都可以实现自动驾驶呢,也就是给所有实现了Vehicle接口的对象添加Intelligent接口?

@Aspect
public class IntroductionsAdvice {
    @DeclareParents(value="com.github.xjs.aopdemo.introduction.Intelligent", defaultImpl=IntelligentCar.class)
    public static Intelligent intelligent;
@Pointcut("this(com.github.xjs.aopdemo.introduction.Vehicle)")
public void vehiclePoint(){
    }
@Before("vehiclePoint() && this(intelligent)")
public void intelligent(Intelligent intelligent) {
        intelligent.selfDriving();
    }
}

@Pointcut拦截所有实现了Vehicle接口的对象,通过DeclareParents添加实现Intelligent接口,则运行以下代码:

@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
@Bean
public IntroductionsAdvice introductionsAdvice(){
return new IntroductionsAdvice();
    }
@Bean
public Vehicle vehicle(){
return new Car();
    }
}
public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        Vehicle vehicle = ctx.getBean(Vehicle.class);
        vehicle.driving();
}

输出结果:

开启无人驾驶了
开车了

Advice的实例化方式

默认情况下,Advice是单例的,SpringAOP也支持perthis 和 pertarget 。

perthis就是给每一个执行业务逻辑的proxy对象创建一个Advice,当第一次执行proxy对象的方法的时候,advice对象被创建出来,二者的生命周期相同,pertarget是给每一个target对象创建一个advice。

举个例子:

@Aspect("perthis(com.xyz.myapp.SystemArchitecture.businessService())")
public class MyAspect {
private int someState;
@Before(com.xyz.myapp.SystemArchitecture.businessService())
public void recordServiceUsage() {
// ...
    }
}

SpringAOP的底层实现

SpringAOP可以使用JDK的动态代理,也可以使用Cglib代理,如果目标对象实现了接口,那就是用jdk的动态代理,如果目标对象没有实现接口,那就是用Cglib,如果想强制使用Cglib,可以是设置:proxy-target-class=true,比如:

<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>

但是要注意:Cglib无法代理final的方法。

由于SpringAOP是基于代理来实现了,因此你要注意被调用的方法是在Proxy对象还是在target对象上。举个例子:

public class SimplePojo implements Pojo {
public void foo() {
//这个方法调用是发生在this对象,也就是target对象内部
this.bar();
    }
public void bar() {
// some logic...
    }
}
public class Main {
public static void main(String[] args) {
        ProxyFactory factory = new ProxyFactory(new SimplePojo());
        factory.addInterface(Pojo.class);
        factory.addAdvice(new RetryAdvice());
        Pojo pojo = (Pojo) factory.getProxy();
// 这里的调用是发生在proxy对象上
        pojo.foo();
    }
}

在foo()内部的调用是不会被代理的。那如果想也走代理该怎么办呢?可以进行如下改造:

public class SimplePojo implements Pojo {
public void foo() {
// this works, but... gah!
        ((Pojo) AopContext.currentProxy()).bar();
    }
public void bar() {
// some logic...
    }
}
public static void main(String[] args) {
        ProxyFactory factory = new ProxyFactory(new SimplePojo());
        factory.addInterface(Pojo.class);
        factory.addAdvice(new RetryAdvice());
//注意这一行
        factory.setExposeProxy(true);
        Pojo pojo = (Pojo) factory.getProxy();
// this is a method call on the proxy!
        pojo.foo();
    }

虽然可以这样做,但是并不推荐,因为与业务代码强耦合了,需要说明的是原生的AspectJ并没有这样的问题,因为它并不是基于代理来实现的。

原文在这里:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop-ataspectj

欢迎扫码加关注:

878a1ea8a60c851d30e085aaa81613b7.png
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值