AOP注解

本文介绍了两种实现面向切面编程(AOP)的方法:XML配置和注解方式。通过具体的示例代码展示了如何定义切入点(pointcut)、前置通知(before advice)等关键概念。
  1. 1. xml 方式配置
  2. <aop:config>  
  3.     <aop:aspectrefaop:aspectref="aspectDef">  
  4.         <aop:pointcutidaop:pointcutid="pointcut1"expression="execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/>  
  5.         <aop:before pointcut-ref="pointcut1" method="beforeAdvice" />  
  6.     </aop:aspect>  
  7. </aop:config>  
  8. 2. 注解方式 
  9. @Component  
  10. @Aspect  
  11. public class AspectDef {  
  12.     //@Pointcut("execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")  
  13.     //@Pointcut("within(com.test.spring.aop.pointcutexp..*)")  
  14.     //@Pointcut("this(com.test.spring.aop.pointcutexp.Intf)")  
  15.     //@Pointcut("target(com.test.spring.aop.pointcutexp.Intf)")  
  16.     //@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")  
  17.     //@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")  
  18.     @Pointcut("args(String)")  
  19.     public void pointcut1() {  
  20.     }  
  21.     @Before(value = "pointcut1()")  
  22.     public void beforeAdvice() {  
  23.         System.out.println("pointcut1 @Before...");  
  24.     }  
AOP(面向切面编程)是一种编程范式,它允许开发者在不修改现有代码的情况下,对程序的功能进行增强。Spring框架提供了丰富的AOP注解来简化AOP的开发。 ### AOP注解类型 Spring AOP提供了多种注解来定义切面、通知等。常见的注解有: - `@Aspect`:用于定义一个切面类,该类包含了多个通知方法,用于在目标方法执行的不同阶段插入额外的逻辑。 - `@Before`:前置通知,在目标方法执行之前执行。 - `@After`:后置通知,在目标方法执行之后执行,无论目标方法是否抛出异常。 - `@AfterReturning`:返回通知,在目标方法正常返回结果后执行。 - `@AfterThrowing`:异常通知,在目标方法抛出异常后执行。 - `@Around`:环绕通知,围绕目标方法执行,可以在目标方法执行前后添加额外的逻辑,甚至可以决定是否执行目标方法。 ### AOP注解使用步骤 1. **引入AOP相关依赖**:在项目的`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.0.4.RELEASE</version> </dependency> ``` 2. **创建目标接口和目标类**:定义一个接口和实现类,作为被增强的目标对象。 ```java // 目标接口 public interface UserService { void addUser(); } // 目标类 import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("添加用户"); } } ``` 3. **创建切面类**:使用`@Aspect`注解定义一个切面类,并在其中定义通知方法。 ```java 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 LogAspect { @Before("execution(* com.example.service.UserService.addUser(..))") public void beforeAddUser() { System.out.println("前置通知:准备添加用户"); } @After("execution(* com.example.service.UserService.addUser(..))") public void afterAddUser() { System.out.println("后置通知:用户添加完成"); } } ``` 4. **配置切面类和目标类**:在Spring配置文件中开启组件扫描和AOP的自动代理。 ```java import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @ComponentScan(basePackages = "com.example") @EnableAspectJAutoProxy public class AppConfig { } ``` 5. **测试**:编写测试代码,验证AOP注解的功能。 ```java import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserService userService = context.getBean(UserService.class); userService.addUser(); context.close(); } } ``` ### 其他相关注解 除了上述AOP注解外,Spring还提供了一些用于标注Bean的衍型注解,如`@Repository`、`@Service`、`@Controller`,它们的功能与`@Component`基本相同,分别用于对DAO、Service以及Web层的Controller进行标注 [^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值