基于AspectJ的注解方式进行AOP开发

-------------------siwuxie095

  

  

  

  

  

  

  

  

  

基于 AspectJ 的注解方式进行 AOP 开发

  

  

1、在Spring 的核心配置文件中配置对象

  

  

  

  

  

2、在Spring 的核心配置文件中通过命名空间 aop 开启

AspectJ 的自动代理

  

  

  

  

  

3、在增强类中使用注解完成AOP 开发

  

  

  

  

4、AOP 注解如下:

  

  

  

  

  

5、具体实现

  

1)编写一个被增强类

  

Book.java:

  

package com.siwuxie095.aop;

  

//被增强类

public class Book {

  

publicvoid add() {

System.out.println("----- add -----");

}

 

}

  

  

  

2)编写一个增强类

  

MyBook.java:

  

package com.siwuxie095.aop;

  

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

  

  

/**

* 增强类

*

* 在增强类上使用注解 @Aspect

*

*/

@Aspect

public class MyBook {

  

/**

* 在方法上使用注解完成增强配置

*

* 其中,value 可以省略不写

*

* @Before("execution(* com.siwuxie095.aop.Book.add(..))")

*/

@Before(value="execution(* com.siwuxie095.aop.Book.add(..))")

publicvoid beforeAdd() {

System.out.println("----- 前置增强 -----");

}

 

@AfterReturning(value="execution(* com.siwuxie095.aop.Book.add(..))")

publicvoid afterAdd(){

System.out.println("----- 后置增强 -----");

}

 

@Around(value="execution(* com.siwuxie095.aop.Book.add(..))")

publicvoid aroundAdd(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println("----- 环绕增强(方法之前) -----");

//执行被增强的方法

proceedingJoinPoint.proceed();

System.out.println("----- 环绕增强(方法之前) -----");

}

 

}

  

  

  

  

3)在配置文件中进行配置

  

applicationContext.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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/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">

  

  

<!-- 配置对象 -->

<beanid="book"class="com.siwuxie095.aop.Book"></bean>

<beanid="myBook"class="com.siwuxie095.aop.MyBook"></bean>

  

  

<!--

开启 AspectJ 的自动代理

 

通过配置织入 @Aspect 切面

-->

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

  

  

</beans>

  

  

  

4)编写一个测试类

  

TestAop.java:

  

package com.siwuxie095.aop;

  

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

  

public class TestAop {

 

/**

* 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 jar 包)

*

* 选中方法名,右键->Run As->JUint Test

*/

@Test

publicvoid testAop() {

 

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

 

Book book=(Book) context.getBean("book");

 

book.add();

}

 

}

  

  

  

5)运行一览:

  

  

  

  

  

  

  

  

  

  

【made by siwuxie095】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值