Atitit aop spring5.2 demo与流程总结
目录
1.3. 设置切面到某个方法上execution(* aoppkg.UserService.login(..) 2
@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;
}
}
//aoppkg.userService.login
@Component
public class UserService {
public int login(final int i) {
return i + 3;
}
@AfterReturning(value = "execution(* aoppkg.UserService.login(..))", returning = "result")
}
@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);
}
}