java aop 使用的简单小例子

例子是通过springboot项目内的自定义注解的方式,通过自定义注解来使用aop对需要执行的方法做一些操作的,废话不多说直接上代码

1.首先pom.xml文件里添加aop的依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
     <version>2.4.4</version>
 </dependency>

2.在Application类上开启注解

@SpringBootApplication
@ComponentScan(basePackages = "com")
@Transactional
//开启aspect 默认未false使用jdk的动态代理  true表示使用cglib动态代理
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class BxApplication {
    public static void main(String[] args) {
        SpringApplication.run(BxApplication.class, args);
    }
}

3.编写自定义注解类,如果想在哪个方法上使用aop,直接在方法上声明该注解便可

//METHOD声明在方法上使用
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Doc {
}

4.编写aop类

@Component
@Aspect
public class Aops {
    //声明切入点,@Doc自定义注解出现在哪个方法上就对哪个方法生效
    @Pointcut("@annotation(com.a1.aop.Doc)")
    private void pointcut(){

    }
    //前置通知,方法执行前通知
    @Before("pointcut()")
    private void befor(){
        System.out.println("befor执行了");
    }
    //后置通知,方法执行后通知
    @After("pointcut()")
    private void after(){
        System.out.println("after执行了");
    }
    //环绕通知,在方法执行前有提示,在方法执行后有提示
    @Around("pointcut()")
    private Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("around执行了");
        Object result = point.proceed();
        System.out.println("around结束了");
        return result;
    }
}

5.编写测试类,本例子是编写了一个Controller层

@Controller
@RequestMapping("/con")
public class TestController {
    @RequestMapping("/aop")
    //自定义注解
    @Doc
    public void test(){
        System.out.println("我就是要执行的方法");
    }
}

6.启动项目:
在浏览器输入http://localhost:8080/con/aop

7.结果是:
在这里插入图片描述

8.小例子总结:
可以看出环绕通知是方法执行前和执行后都有通知提示,并且开始前的通知在前置通知之前,结束后的通知在后置通知之后,可以根据实际情况选择需要的通知方法。

有写的不清楚的地方还请大家不吝赐教,共同探讨、共同学习、共同成长!

AOP(面向切面编程)在Java的实现方式是通过使用AOP框架来实现的。常用的AOP框架包括Spring AOP和AspectJ。 在Java使用AOP,首先需要定义切面(Aspect),切面是一组横切关注点(cross-cutting concerns)的模块化,比如日志、性能统计、安全等。然后使用切点(Pointcut)来定义哪些类和方法需要被切面引入(Intercept)。接着定义通知(Advice),通知就是在切点处执行的逻辑代码,比如在方法调用前、调用后、调用前后等等。最后将切面、切点、通知组合起来形成切面配置(Aspect Configuration)。 下面是一个简单的Spring AOP实现: 1. 定义切面类: ``` @Component @Aspect public class LoggingAspect { @Before("execution(* com.example.myapp.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before method execution: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.myapp.service.*.*(..))") public void logAfter(JoinPoint joinPoint) { System.out.println("After method execution: " + joinPoint.getSignature().getName()); } } ``` 2. 在配置文件定义切面配置: ``` <aop:aspectj-autoproxy/> <bean id="loggingAspect" class="com.example.myapp.aspect.LoggingAspect"/> <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.myapp.service.*.*(..))"/> <aop:before pointcut-ref="serviceMethods" method="logBefore"/> <aop:after pointcut-ref="serviceMethods" method="logAfter"/> </aop:aspect> </aop:config> ``` 这个例子定义了一个LoggingAspect切面,拦截com.example.myapp.service包所有方法的执行前和执行后,并打印方法名。然后通过配置文件将LoggingAspect和切点组合起来。其,pointcut用于定义切点,before和after分别定义了前置通知和后置通知。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值