Springboot使用Aspectj实现AOP面向切面编程

要在 Springboot中声明 AspectJ 切面, 需在 IOC 容器中将切面声明为 Bean 实例 即加入@Component 注解;当在 Spring IOC 容器中初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.
在 AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java 类.

引入jar包:

        网上都是说springboot使用Aspectj做面向切面编程的时候,只需要引入下面jar包依赖即可

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
但是我去编写的时候,单单引入 spring-boot-starter-aop 的jar依赖的时候,像@Component、@Aspect等這些註解都不能使用,後來發現缺少aspectjweaver 这么个jar包,最后引入了下面的jar才解決問題
    <dependency>
	<groupId>aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.5.3</version>
    </dependency> 

网上也有说要在application.properties中添加spring.aop.auto=true这个配置,才能开启Aspectj注解的扫面,但是我去查询了springboot全局配置文件,里面默认配置为true(spring.aop.auto=true # Add @EnableAspectJAutoProxy),所以我没有去做添加,功能没有问题,切面能正常实现。


最后补充一点小知识:
 AspectJ 支持 5 种类型的通知注解
1)@Before:  前置通知:在方法执行之前执行的通知
2)@After: 后置通知, 在方法执行之后执行 , 即方法返回结果或者抛出异常的时候, 下面的后置通知记录了方法的终止.
3)@AfterRunning: 返回通知, 在方法返回结果之后执行
        ps:无论方法是正常返回还是抛出异常, 后置通知都会执行. 如果只想在方法返回的时候记录日志, 应使用返回通知代替后置通知.

4)@AfterThrowing: 异常通知, 在方法抛出异常之后

5) @Around: 环绕通知, 围绕着方法执行(即方法前后都有执行)
       环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.


下面是我写的一些通知的实例,大家可以参考一下

        /*
	    标识这个方法是个前置通知,  切点表达式表示执行任意类的任意方法.
	    第一个 * 代表匹配任意修饰符及任意返回值, 
	    第二个 * 代表任意类的对象,
	    第三个 * 代表任意方法,
	    参数列表中的 ..  匹配任意数量的参数
	 */
 
    //@Before:  前置通知
    @Before("execution (* com.lc.project..controller..*.*(..))")
    public void beforeMethod(JoinPoint joinPoint){
	    String methodName = joinPoint.getSignature().toString();
	    Object result= Arrays.asList(joinPoint.getArgs());
            System.out.println("The method name:"+methodName+"--value:"+result);
    }

    //@After: 后置通知
    @After("execution (* *.*(..))")
    public void afterMethod(JoinPoint joinPoint){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method name:"+methodName+ " ends");
    }
    //@AfterRunning: 返回通知
    @AfterReturning(value="execution (* *.*(..))",returning="result")
    public void afterReturningMethod(JoinPoint joinPoint,Object result){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method name:"+methodName+ " ends and result="+result);
    }
    //@AfterThrowing: 异常通知
    @AfterThrowing(value="execution (* *.*(..))",throwing="e")
    public void afterReturningMethod(JoinPoint joinPoint,Exception e){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method name:"+methodName+ " ends and result="+e);
    }					
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的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(&quot;execution(* com.example.service.*.*(..))&quot;) public void beforeAdvice(JoinPoint joinPoint) { System.out.println(&quot;Before method: &quot; + joinPoint.getSignature().getName()); } @After(&quot;execution(* com.example.service.*.*(..))&quot;) public void afterAdvice(JoinPoint joinPoint) { System.out.println(&quot;After method: &quot; + joinPoint.getSignature().getName()); } } ``` 然后,创建一个测试服务类 `UserService`,用于演示AOP的应用: ```java import org.springframework.stereotype.Service; @Service public class UserService { public void createUser(String username) { System.out.println(&quot;Creating user: &quot; + username); } public void deleteUser(String username) { System.out.println(&quot;Deleting user: &quot; + 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(&quot;John&quot;); userService.deleteUser(&quot;John&quot;); } } ``` 在上述示例中,`LoggingAspect` 切面使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。 当运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。 这是一个简单的AOP面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值