AOP面向切面编程

1,注入依赖

  <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>

2,编写配置类


@Aspect//标识切面类
@Component
public class AopConfig {

    //切点
    @Pointcut("execution(public * com.tianji.aopdemo.service..*.*(..))")
    private void pt(){}


//    @Before()前置通知
//    @After()后置通知
//    @AfterThrowing//异常通知
//    @AfterReturning//最终通知
    @Around("pt()")//环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("开始时间:"+new Date());
        Object[] args= pjp.getArgs();
        Object ret = pjp.proceed(args);//执行目标方法

        //如果有@PrintTime注解 打印注解的title属性值
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            PrintTime methodAnnotation = method.getAnnotation(PrintTime.class);
            if (methodAnnotation != null) {
                System.out.println(methodAnnotation.title());
            }
        }

        System.out.println("结束时间:"+new Date());
        return ret;
    }

}

3,只对带有@PrintTime注解的方法进行通知的方法:

    //切点
//@Pointcut("execution(public * com.tianji.aopdemo.service..*.*(..))")
//private void pt(){}

    //切点
    @Pointcut("@annotation(com.tianji.aopdemo.config.PrintTime)") //注解的目录
    private void pt() {}

4,还可以直接写成:(把通知和切入点结合)

    @Around("@annotation(printTime)")
    public Object around(ProceedingJoinPoint pjp,PrintTime printTime) throws Throwable {
        System.out.println("开始时间:" + new Date());
        Object[] args = pjp.getArgs();
        Object ret = pjp.proceed(args);//执行目标方法

        System.out.println(printTime.title());//打印注解的title属性

        System.out.println("结束时间:" + new Date());
        return ret;
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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("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面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Winter.169

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值