在idea中利用Spring进行面向切面编程(AOP)的一个例子

(1)在idea中新建立一个maven项目aopAspectj,,编写POM文件,导入jar包:

<dependencies>
    <!--aspectj依赖-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.8.10</version>
    </dependency>

    <!--spring相关包-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>

</dependencies>
(2)编写一个接口:Performance(可以代表任何类型的现场表演)
public interface Performance {
    public void perform();
}

(3)实现接口Performance方法。创建了一个歌舞剧表演

public class Theatre implements Performance{
    public void perform(){
        System.out.println("50个人正在表演舞台剧");
    }
}


(4)使用注解@Aspect来定义切面。在表演之前的动作有silenceCellPhonestakeSeats。表演成功后的动作:applause。表演异常的动作有demandRefund

@Aspect //表明该类别是一个切面
public class Audience {

    @Pointcut("execution(** Performance.perform(..))")  //定义命令的切点
    public void performance(){}

    @Before("performance()")    //在表演之前
    public void silenceCellPhones(){
        System.out.println("Silencing cell phones");
    }

    @Before("performance()")    //在表演之前
    public void takeSeats(){
        System.out.println("Taking seats");
    }

    @AfterReturning("performance()")    //表演之后
    public void applause(){
        System.out.println("CLAP CLAP CLAP!!!");
    }

    @AfterThrowing("performance()")
    public void demandRefund(){    //表演失败之后
        System.out.println("Demanding a refund");
    }
}


其中@Pointcut是用来定义performance(),使它可以代表含义"execution(** Performance.perform(..))"



(5)然后创建配置类ConcertConfig,用来把Aspectj自启动代理。其中@EnableAspectJAutoProxy来启动Aspectj代理。

@Configuration
@EnableAspectJAutoProxy //启用Aspectj自动代理
public class ConcertConfig {

    @Bean   //声明了一个切面bean
    public Audience audience(){
        return new Audience();
    }

    @Bean
    public Performance theatre(){
        return new Theatre();
    }
}


(6)测试主类PerformanceTest

ublic class PerformanceTest {
    public static void main(String[] args){
        //加载java配置类获取Spring应用上下文
        ApplicationContext ac=new AnnotationConfigApplicationContext(ConcertConfig.class);
        //获取播放器
        Performance pf=ac.getBean(Performance.class);
        //播放
        pf.perform();
    }
}


(7)结果显示:







  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

洛克-李

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

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

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

打赏作者

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

抵扣说明:

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

余额充值