首页 > 技术文章 > Spring AOP配置简单记录(注解及xml配置方式)

new-life 2018-08-30 16:12 原文

在了解spring aop中的关键字(如:连接点(JoinPoint)、切入点(PointCut)、切面(Aspact)、织入(Weaving)、通知(Advice)、目标(Target)等)后进行了简单使用测试。

1.在spring的xml配置文件中添加代码,开启aop注解自动代理

<!-- 启动aspectJ自动代理 -->
<aop:aspectj-autoproxy />

 

2.创建一个方法作为连接点:

public class TestService {
    
    public void method1(){
        System.out.println("执行method1");
    }
}

 

3.创建切面类(注解方式配置):

@Aspect// 注解该类为切面类
@Component
public class LogIntercepter {
    
    // 若要以字符串形式表示执行目标条件必须使用final修饰
    private final String EXEC = "execution(* com.lwl.service..*.*(..))";
    
    // 执行顺序:环绕通知(前),前置通知,环绕通知(后),后置通知,返回后通知
    @Pointcut("execution(* com.lwl.service..*.*(..))")// 切入点
    public void pointcutMethod(){};
    
    @Before("pointcutMethod()")// 方法执行前执行
    public void before(){
        System.out.println("method before");
    }
    
    @After("pointcutMethod()")// 方法执行后执行
    public void after(){
        System.out.println("method after");
    }
    
    @AfterReturning("pointcutMethod()")// 方法返回后执行
    public void afterReturning(){
        System.out.println("method afterReturning");
    }
    
    @AfterThrowing("pointcutMethod()")// 方法抛异常后执行
    public void afterThrowing(){
        System.out.println("method afterThrowing");
    }
    
    @Around("pointcutMethod()")// 环绕通知
    public void around(ProceedingJoinPoint pr){
        System.out.println("method around bg");
        try {
            pr.proceed();// 执行目标方法
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("method around ed");
    }
}

调用method1()后通知的打印先后顺序结果为:

method around bg
method before
执行method1
method around ed
method after
method afterReturning

 

 

Spring AOP xml方式配置,在Spring的xml配置文件中加入(备忘):


<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"
     xsi:schemaLocation="http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1.注册实体类(目标方法所在类) -->
<bean id="userDao" class="com.huitong.Dao3.UserDao"></bean>
<!-- 2.注册AOP切面实体类(通知方法所在类) -->
<bean id="aop" class="com.huitong.Dao3.Aop"></bean>
      
<aop:config>
    <!-- 3.切入点配置 -->
    <aop:pointcut expression="execution(* com.huitong.Dao3.UserDao.*(..))" id="pt"/>
    <!-- 4.切面配置 -->
    <aop:aspect id="asp" ref="aop">
        <!-- 5.通知配置 -->
        <aop:around method="around" pointcut-ref="pt"/>
        <aop:before method="before" pointcut-ref="pt"/>
    </aop:aspect>
</aop:config>

 

AOP相关的术语:

    • 通知: 通知定义了切面是什么以及何时使用的概念。Spring 切面可以应用5种类型的通知:

      • 前置通知(Before):在目标方法被调用之前调用通知功能。
      • 后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么。
      • 返回通知(After-returning):在目标方法成功执行之后调用通知。
      • 异常通知(After-throwing):在目标方法抛出异常后调用通知。
      • 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。
    • 连接点:是在应用执行过程中能够插入切面的一个点。

    • 切点: 切点定义了切面在何处要织入的一个或者多个连接点。
    • 切面:是通知和切点的结合。通知和切点共同定义了切面的全部内容。
    • 引入:引入允许我们向现有类添加新方法或属性。
    • 织入:是把切面应用到目标对象,并创建新的代理对象的过程。切面在指定的连接点被织入到目标对象中。在目标对象的生命周期中有多个点可以进行织入: 
      • 编译期: 在目标类编译时,切面被织入。这种方式需要特殊的编译器。AspectJ的织入编译器就是以这种方式织入切面的。
      • 类加载期:切面在目标加载到JVM时被织入。这种方式需要特殊的类加载器(class loader)它可以在目标类被引入应用之前增强该目标类的字节码。
      • 运行期: 切面在应用运行到某个时刻时被织入。一般情况下,在织入切面时,AOP容器会为目标对象动态地创建一个代理对象。SpringAOP就是以这种方式织入切面的。

推荐阅读