不依赖Spring使用AspectJ达到AOP面向切面编程
时间: 2024-02-06 07:03:48 浏览: 146
使用AspectJ实现AOP面向切面编程不需要依赖Spring框架,可以独立实现。下面是实现AOP的基本步骤:
1. 引入AspectJ库:在项目中引入AspectJ的相关库,例如aspectjrt.jar和aspectjweaver.jar。
2. 定义切面类:在AspectJ中,切面类是一个普通的Java类,用于封装横切关注点。切面类需要使用@Aspect注解来标识。
3. 定义切点表达式:切点表达式用于定义哪些方法需要被织入横切逻辑。切点表达式需要使用@Pointcut注解来标识。
4. 定义通知:通知是横切逻辑的具体实现。AspectJ支持5种类型的通知:@Before、@After、@AfterReturning、@AfterThrowing和@Around。
5. 组装切面:将切面类和通知组装起来,可以使用@Aspect注解中的“value”属性来指定切点表达式,然后在通知方法上使用@Pointcut注解来引用切点表达式。
6. 启用AspectJ:需要在应用程序中启用AspectJ,可以使用AspectJ提供的LoadTimeWeaver来实现。
通过以上步骤,就可以使用AspectJ实现AOP面向切面编程。
相关问题
AOP面向切面编程的测试代码以及依赖导入
以下是一个使用Spring框架实现AOP面向切面编程的测试代码示例。
首先,创建一个切面类 `LoggingAspect`,用于定义切面逻辑:
```java
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method: " + joinPoint.getSignature().getName());
}
@After("execution(* com.example.service.*.*(..))")
public void afterAdvice(JoinPoint joinPoint) {
System.out.println("After method: " + joinPoint.getSignature().getName());
}
}
```
然后,创建一个测试服务类 `UserService`,用于演示AOP的应用:
```java
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void createUser(String username) {
System.out.println("Creating user: " + username);
}
public void deleteUser(String username) {
System.out.println("Deleting user: " + username);
}
}
```
最后,创建一个Spring Boot应用程序,并在启动类中进行配置:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@EnableAspectJAutoProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
UserService userService = context.getBean(UserService.class);
userService.createUser("John");
userService.deleteUser("John");
}
}
```
在上述示例中,`LoggingAspect` 切面类使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。
为了能够运行Spring Boot应用程序,需要在项目的构建工具(如Maven或Gradle)的配置文件中添加Spring相关的依赖。
如果使用Maven,可以在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
```
如果使用Gradle,可以在build.gradle文件中添加以下依赖:
```groovy
implementation 'org.springframework.boot:spring-boot-starter:2.5.3'
```
这样,就完成了AOP面向切面编程的测试代码的实现和依赖导入。运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。
java aop面向切面编程
### Java AOP(面向切面编程)概念
Java AOP 是一种编程范式,旨在通过分离横切关注点来提高模块化程度。横切关注点是指那些影响多个类的功能,如事务管理、日志记录和安全性等。这些功能通常散布于整个应用的不同部分,难以维护和修改。
Spring AOP 提供了声明式和编程式的 AOP 实现,其中较常用的是声明式 AOP[^1]。这种技术允许开发者定义特定的行为——称为“方面”,并将其应用于程序的其他部分而不改变原有业务逻辑代码。
### 创建增强方法所在类 (即切面)
为了实现增强功能,需要创建一个包含增强逻辑的方法所在的类:
```java
package Semester_4.AOP.JDKDynamicProxy.intensifier;
public class MyAspect {
public void checkPermissions(){
System.out.println("模拟检查权限的过程……");
}
public void log(){
System.out.println("模拟记录权限的过程……");
}
}
```
上述代码展示了如何在一个名为 `MyAspect` 的类中定义两个简单的增强函数:一个是用于模拟权限验证过程,另一个则是用来打印日志信息[^2]。
### 日志记录功能的应用场景
在实际开发过程中,利用 Spring AOP 可以方便地实现在不侵入原业务逻辑的情况下添加额外行为的需求。比如,在应用程序内部自动捕获所有公共接口调用的信息,并将它们写入到文件或其他存储介质里去。这不仅简化了编码工作量还提高了系统的可读性和灵活性[^3]。
### 定义切面类与配置切入点表达式
下面是一个具体的例子展示怎样基于 AspectJ 注解风格来构建一个完整的切面组件:
```java
@Aspect
@Component
public class LogAspect {
/**
* Before注解: 前置通知,在目标方法执行之前执行;
* value参数: 切入点表达式,指定哪些方法需要被植入增强逻辑;
*/
@Before(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
public void beforeMethod() {
//前置通知的增强逻辑
System.out.println("即将执行计算操作...");
}
/* 还可以有其他的环绕通知(@Around), 后置返回通知 (@AfterReturning),
抛出异常后的处理(@AfterThrowing),以及最终的通知(@After) */
}
```
这段代码片段说明了一个典型的切面类结构及其基本组成部分。这里使用了 `@Aspect` 和 `@Component` 来标记该类为一个切面组件;并通过 `@Before` 注解读取切入点表达式从而决定何时触发相应的增强逻辑[^4]。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)