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

时间:2023-03-08 20:40:29

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

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

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

 
 

 
 

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

 
 

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

 
 

 
 

 
 

 
 

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

AspectJ 的自动代理

 
 

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

 
 

 
 

 
 

 
 

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

 
 

 
 

 
 

 
 

4、AOP 注解如下:

 
 

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

 
 

 
 

 
 

 
 

5、具体实现

 
 

(1)编写一个被增强类

 
 

Book.java:

 
 

package com.siwuxie095.aop;

 
 

//被增强类

public class Book {

 
 

public
void 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(..))")

public
void beforeAdd() {

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

}

 

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

public
void afterAdd(){

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

}

 

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

public
void aroundAdd(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

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

//执行被增强的方法

proceedingJoinPoint.proceed();

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

}

 

}

 
 

 
 

 
 

 
 

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

 
 

applicationContext.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"

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">

 
 

 
 

<!-- 配置对象 -->

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

<bean
id="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

public
void testAop() {

 

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

 

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

 

book.add();

}

 

}

 
 

 
 

 
 

(5)运行一览:

 
 

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

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

相关文章