Atitit aop spring5.2 demo与流程总结 目录 1.1. 定义切面MyAllAspect 1 1.2. 定义普通类型userservice 1 1.3. 设置切面到某个方法上ex

Atitit aop spring5.2 demo与流程总结

 

目录

1.1. 定义切面MyAllAspect 1

1.2. 定义普通类型userservice 1

1.3. 设置切面到某个方法上execution(* aoppkg.UserService.login(..) 2

2. 配置与启动 2

 

 

    1. 定义切面MyAllAspect 

 

@Component

 

@Aspect // AOP 切面u

 

@SuppressWarnings("all")

public class MyAllAspect {

 

 

 

//   切入点(Pointcut):用于定义通知应该切入到哪些连接点上

@AfterReturning(value = "execution(* aoppkg.UserService.login(..))", returning = "result")

public Object afterReturning(JoinPoint joinPoint, Object result) {

 

System.out.println("log.....");

 

return result;

}

}

 

    1. 定义普通类型userservice

 

//aoppkg.userService.login

 

@Component

public class UserService {

 

public int login(final int i) {

 

 

return i + 3;

 

}

 

 

    1. 设置切面到某个方法上execution(* aoppkg.UserService.login(..)

 

@AfterReturning(value = "execution(* aoppkg.UserService.login(..))", returning = "result")

}

  1. 配置与启动

 

@EnableTransactionManagement

@EnableAspectJAutoProxy

@Configuration /** 该注解表示这个类是一个Spring的配置类 **/

@ComponentScan(basePackages = {

"aoppkg" }) /***

 * 该注解表示启用spring的组件扫描功能,并且配置了扫描包net.xqlee.project.

 * demo下的所有类

 **/

public class AppConfig implements TransactionManagementConfigurer {

 

 

 

 

 

public class start {

 

public static void main(String[] args) {

 

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

// 获取通过注解注入容器的UserService

UserService userService1 = context.getBean(UserService.class);

// 调用userService的方法执行

  userService1.login(6);

 

}

 

}

 

原文地址:https://www.cnblogs.com/attilax/p/15197454.html