AOP(面向切面编程)

泛型依赖注入:
这里写图片描述

面向切面要解决的问题:
1、代码混乱:越来越多的非业务需求(日志、验证等)的加入使得业务方法变得急剧膨胀,每个方法在处理核心业务逻辑的同时还要兼顾其他多个关注点。
2、代码分散:以日志需求为例,为了实现这个单一需求,不得不在模块或方法中书写重复的日志代码,如果日志需求一旦发生变化,不得不修改所有的日志代码

使用动态代理解决上述问题:
代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理对象取代原始对象,任何对原始对象的调用都要经过代理对象,代理对象决定将方法调用转到原始对象上面

面向切面原理(基于注解的方式和xml方式):
1、AOP(Aspect Oriented Programming)是一种新的方法论,是对传统OOP的补充
2、AOP的主要编程对象是“切面(Aspect)”,而切面模块化横切关注点
3、在进行AOP编程时,任然需要定义公共功能,但可以明确的定义这个公共功能在哪里以及如何应用。不必修改受影响的类,这样一来横切关注点就被模块化到对象(切面)里面。

AOP的好处:
1、每个事物逻辑位于一个位置,代码不分散,便于维护和升级
2、业务模块更简洁,只包含业务核心代码
实现步骤:
这里写图片描述
这里写图片描述
面向切面有五个通知类型:
1、环绕通知:包含前置、后置、返回、异常通知,类似于动态代理的全过程:
这里写图片描述
切面优先级问题:
注解:order:越小,优先级越高

定义一个方法。用于声明切入点表达式,一般地,该方法中不在写其他的代码
@Pointcut(“execution(* com.spring.aop.impl..(int,int))”)
public void declareJoinPoint(){}
@Before(“declareJoinPoint()”) // 引用了声明的切入点表达式(不同包下,可用包路径)
public void boforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List args = Arrays.asList(joinPoint.getArgs());
System.out.println(“The method “+methodName+” begin with “+ args);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的AOP面向切面编程的测试代码示例,使用Spring框架实现: 首先,创建一个切面类 `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` 类中的所有方法。 当运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。 这是一个简单的AOP面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值