基于注解的AOP开发

x33g5p2x  于2022-08-17 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(431)

基于注解的AOP开发

编写测试 

注解配置AOP详解

注解通知的类型

切点表达式的抽取

 基于注解的AOP开发

快速入门,基于注解的aop开发步骤
①创建目标接口和目标类(内部有切点)

②创建切面类(内部有增强方法)

③将目标类和切面类的对象创建权交给spring

④在切面类中使用注解配置织入关系

⑤在配置文件中开启组件扫描和AOP的自动代理

⑥测试

编写测试 

其中Target类下

package anno;
import org.springframework.stereotype.Component;

//交给spring容器,起个名为target
@Component("target")
public class Target implements TargetInterface {

    public void save() {
        System.out.println("save running。。。");
    }
}

Interface类下

package anno;

public interface TargetInterface {
    public void save();
}

MyAspect切面类下

package anno;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//交给spring容器
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
    @Before("execution(* anno.*.*(..))")
   public void before(){
       System.out.println("前置增强....");
   }

}

AnnoTest测试类下

package anno;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_anno.xml")
public class AnnoTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }

}

applicationContext_anno.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"
       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.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

<!--组件扫描-->
    <context:component-scan base-package="anno"/>

<!--aop自动代理,加上才会识别这些通知的注解-->
    <aop:aspectj-autoproxy/>

</beans>

运行结果 

 注解配置AOP详解

注解通知的类型

通知的配置语法:@通知注解("切点表达式")

 切点表达式的抽取

同xml配置aop一样。我们可以将切点表达式抽取,抽取方式是在切面内定义方法,早该方法上使用**@Pointcut注解**定义切点表达式,然后在在增强注解中进行引用。

切面类中

package anno;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//交给spring容器
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
    @Before("execution(* anno.*.*(..))")
   public void before(){
       System.out.println("前置增强....");
   }
   //引入切点表达式方法
   @AfterReturning("pointcut()")//或者写MyAspect.pointcut()
    public void afterReturn(){
        System.out.println("后置增强....");
    }

   //定义切点表达式方法
    @Pointcut("execution(* anno.*.*(..))")
    public void pointcut(){ }

}

运行结果 

相关文章