SpringAOP - 注解实现AOP

注解实现AOP

配置类

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
@EnableAspectJAutoProxy
public class AnnoAOPConfig {
@Bean
public LogAnno logAnno() {
return new LogAnno();
}
@Bean
public LoginService loginService() {
return new LoginServiceImpl();
}
}

注意@EnableAspectJAutoProxy开启自动Proxy

如果是使用xml进行配置,也需要开启自动Proxy。

业务类应该返回相应的接口,动态路由的底层实现即是对接口进行调用。

切面类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Aspect
@Component
public class LogAnno {
@Before("execution(* com.edlison.design.spring.aop.style_anno.LoginServiceImpl.*(..))")
public void before() {
System.out.println("[Log] Before...");
}
@After("execution(* com.edlison.design.spring.aop.style_anno.LoginServiceImpl.*(..))")
public void after() {
System.out.println("[Log] After...");
}
@Around("execution(* com.edlison.design.spring.aop.style_anno.LoginServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("[Log] Point Start");
joinPoint.proceed();
System.out.println("[Log] Point End");
System.out.println("joinPoint.getSignature() = " + joinPoint.getSignature());
}
}

@Aspect实际上是引用的org.aspectj包。

切面类@Component注册为Bean

问题

  • 执行先后顺序与视频中的不一样,与Python装饰器的不一样,使用配置类的原因?
  • @Component@Bean重复?