基于注解的AOP开发

目录

 基于注解的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(){ }


}

运行结果 

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执久呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值