Spring AOP

pring实现动态代理配置是有两种配置文件:

1、   xml文件方式;

2、   annotation方式(使用AspectJ类库实现的。)

一、       AOP配置annotation方式

(一)  搭建annotation开发环境

首先:需要在配置文件中加入@AspectJ标签

<aop:aspectj-autoproxy/>

自动帮我产生代理

注意:Spring默认并没有加入aopxsd文件,因为我们需要手动加入(红色部分)

<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-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="com.wjt276"/>

    <aop:aspectj-autoproxy/>

</beans>

 

       另外需要引用aspectJjar包:

                                          aspectjweaver.jar

                                          aspectjrt.jar

(二)  aspectJ类库

AspectJ是一个专门用来实现动态代理(AOP编程)的类库

AspectJ是面向切面编程的框架

Spring使用就是这个类库实现动态代理的

(三)  AOPannotation实例

要求:在执行save()方法之前加入日志逻辑

1、   spring的配置文件同上面的

2、   model类、dao层类、service层类都与上面天下一致

3、   切面类(LogInterceptor)

 

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

@Aspect

@Component

public class LogInterceptor {

 

    @Before("execution(public void com.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User))")

    public void before(){

       System.out.println("method start...");

    }  

}

       结果:这样在运行public void com.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User)方法之前就会先执行这个逻辑了。

    注意:

    1@Aspect:意思是这个类为切面类

    2@Componet:因为作为切面类需要Spring管理起来,所以在初始化时就需要将这个类初始化加入Spring的管理;

    3@Befoe:切入点的逻辑(Advice)

    4execution…:切入点语法

 

(四) 

三个连接点(切入点)

AspectJ 的专业术语

1、  JoinPoint

切入面

连接点(切入点)

 

程序执行过程

 

 

 

 

 

 

 


2、  PointCut

切入点人集合

当需要定义一个切入点时,则需要使用这个

@Pointcut("execution(* com.xyz.someapp.service.*.*(..))")

  public void businessService() {}

 

3、  Aspect

切面

4、  Advice

切入点的逻辑

例如上例中的@Before

5、  Target

被代理对象

6、  Weave

织入

 

(五)  织入点语法

1、    无返回值、com.wjt276.dao.impl.UserDaoImpl.save方法 参数为User

execution(public void com.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User))

2、    任何包、任何类、任何返回值、任何方法的任何参数

execution(public * *(..))

3、    任何包、任何类、任何返回值、任何set开头方法的任何参数

execution(* set*(..))

4、    任何返回值、com.xyz.service.AccountService类中的任何方法、任何参数

execution(* com.xyz.service.AccountService.*(..))

5、    任何返回值、com.xyz.service包中任何类中的任何方法、任何参数

execution(* com.xyz.service.*.*(..))

6、    任何返回值、com.xyz.service包中任何层次子包(..)、任何类、任何方法、任何参数

execution(* com.xyz.service..*.*(..))

7、    void !void(void)

execution(public void com.xyz.service..*.*(..))

execution(public !void com.xyz.service..*.*(..))

注意:上以是AspectJ的织入点语法,Spring AOP也实现了自己的织入点语法,同样可以使用

within(com.xyz.service.*)

 

within(com.xyz.service..*)

 

this(com.xyz.service.AccountService)

 

target(com.xyz.service.AccountService)

 

args(java.io.Serializable)

 

@target(org.springframework.transaction.annotation.Transactional)

 

@within(org.springframework.transaction.annotation.Transactional)

 

@annotation(org.springframework.transaction.annotation.Transactional)

 

@args(com.xyz.security.Classified)

 

bean(tradeService)

 

bean(*Service)

(六)  Advice

1、     @Before

执行方法之前

@Aspect

public class BeforeExample {

  @Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doAccessCheck() { // ... }}

 

@Aspect

public class BeforeExample {

  @Before("execution(* com.xyz.myapp.dao.*.*(..))")

  public void doAccessCheck() { // ... }}

2、     @ AfterReturning

方法正常执行完之后

@Aspect

public class AfterReturningExample {

  @AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doAccessCheck() { // ... }}

 

@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("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doRecoveryActions() { // ... }}

 

@Aspect

public class AfterThrowingExample {

  @AfterThrowing(

    pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",

    throwing="ex")

  public void doRecoveryActions(DataAccessException ex) { // ... }}

4、      @After (finally)

方法抛出异常被catch之后,需要进行的部分(相当于finally功能)

@Aspect

public class AfterFinallyExample {

  @After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doReleaseLock() { // ... }}

5、     @ Around

在方法之前和之后都要加上

但是需要一个参数ProceedingJoinPoint,并者需要Object retVal = pjp.proceed();

和返回return retVal;

@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; }}

 

(七)  Pointcut

当多个Advice个有相同的织入点。那么我们可以定义一个织入点集合,在需要使用的地方,调用就可以了。

例如:

@Aspect

@Component

public class LogInterceptor {

 

    @Pointcut("execution(public * com.wjt276.dao..*.*(..))")

    public void myMethod(){};

   

    @Before(value="myMethod()")

    public void before(){

       System.out.println("method start...");

    }

 

    @AfterReturning("myMethod()")

    public void afterReturning(){

       System.out.println("method after returning...");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值