文章目录
开启自动织入支持
在XML中开启
添aop标签
<aop:aspectj-autoproxy />
开启注解支持,同时强制指定代理机制为cglib
<aop:aspectj-autoproxy proxy-target-class="true" />
通过注解开启
@Configuration
// 开启注解支持,同时强制指定代理机制为cglib
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class MyAOPConf {}
@Component
@Aspect //表示这是一个切面类,里面的方法都是像打印日志等围绕业务方法的方法
注解方式入口类
@Configuration //相当生成一个XML文件
@ComponentScan(basePackages = "com.lanou3g.spring.simple.say")//扫描路径
@EnableAspectJAutoProxy //开启对AOP相关注解的处理
public class AppByAnnotation {
...
}
注解方式定义切面类
@Pointcut:切入点的注解
由于@Aspect注解没有让Spring作为组件bean扫描的能力,所以我们需要额外添加@Component注解
@Aspect // 表示该类是一个切面
@Component // Aspect切面首先必须也是一个普通的bean
public class MethodInOutAspect