会把 AspectJ的LTW放到类加载器当中,所有的类都将能匹配到定义好的 Pointcut
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:load-time-weaver /> <bean id="processor" class="org.springbyexample.aspectjLoadTimeWeaving.Processor" /> </beans> 复制代码
aspectJ会自动加载 META-INF/aop.xml,有关 weaver 和 aspects 的配置示例
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> <aspectj> <weaver> <!-- only weave classes in this package --> <!-- 声明给 weaver 哪里有 weave 要处理 --> <include within="org.springbyexample.aspectjLoadTimeWeaving.*" /> </weaver> <aspects> <!-- use only this aspect for weaving --> <!-- 这里是提议 Advice --> <aspect name="org.springbyexample.aspectjLoadTimeWeaving.PerformanceAdvice" /> </aspects> </aspectj> 复制代码
@Aspect public class PerformanceAdvice { @Pointcut("execution(public * org.springbyexample.aspectjLoadTimeWeaving..*.*(..))") public void aspectjLoadTimeWeavingExamples() { } @Around("aspectjLoadTimeWeavingExamples()") public Object profile(ProceedingJoinPoint pjp) throws Throwable { final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringType()); StopWatch sw = new StopWatch(getClass().getSimpleName()); try { sw.start(pjp.getSignature().getName()); return pjp.proceed(); } finally { sw.stop(); logger.debug(sw.prettyPrint()); } } } 复制代码