最近有个需求 -- 记录应用中某些接口被调用的轨迹, 说白了, 记录下入参, 出参等即可.
我选用 ApsectJ 解决这个问题, 前期讨论说在接口层埋点, 但这样有个问题, 代码侵入比较严重, 需要修改每个需要关注的接口实现类. 经过一番讨论, 决定使用 AOP 拦截所有这样的接口.
后面又有个新的要求 -- 沙箱环境拦截, 生产环境不予拦截.
这样就有个眼前的问题需要我们解决, 就是同一份应用包如何区分沙箱环境和生产环境并执行不同的行为. 同事提醒我可以考虑 Spring 的 LTW, 即 Load Time Weaving.
在 Java 语言中, 从织入切面的方式上来看, 存在三种织入方式: 编译期织入, 类加载期织入和运行期织入. 编译期织入是指在 Java 编译期, 采用特殊的编译器, 将切面织入到 Java 类中; 而类加载期织入则指通过特殊的类加载器, 在类字节码加载到 JVM 时, 织入切面; 运行期织入则是采用 CGLib 工具或 JDK 动态代理进行切面的织入.
AspectJ 采用编译期织入和类加载期织入的方式织入切面, 是语言级的 AOP 实现, 提供了完备的 AOP 支持. 它用 AspectJ 语言定义切面, 在编译期或类加载期将切面织入到 Java 类中.
AspectJ 提供了两种切面织入方式, 第一种通过特殊编译器, 在编译期, 将 AspectJ 语言编写的切面类织入到 Java 类中, 可以通过一个 Ant 或 Maven 任务来完成这个操作; 第二种方式是类加载期织入, 也简称为 LTW(Load Time Weaving).
如何使用 Load Time Weaving? 首先, 需要通过 JVM 的 - javaagent 参数设置 LTW 的织入器类包, 以代理 JVM 默认的类加载器; 第二, LTW 织入器需要一个 aop.xml 文件, 在该文件中指定切面类和需要进行切面织入的目标类.
下面我将通过一个简单的例子在描述如何使用 LTW.
例子所作的是记录被调用方法的执行时间和 CPU 使用率. 其实这在实际生产中很有用, 与其拉一堆性能测试工具, 不如动手做个简单的分析切面, 使我们能很快得到一些性能指标. 我指的是没有硬性的性能测试需求下.
首先我们编写一个被织入的受体类, 也就是被拦截的对象.
- public class DemoBean {
- public void run() {
- System.out.println("Run");
- }
- }
接着, 我们编写分析方法执行效率的切面.
- <span style="font-family:'Courier New'; color: #646464; font-size: x-small;">
- @Aspect public class ProfilingAspect {br/>@Around("profileMethod()") public
- Object profile(ProceedingJoinPoint pjp) throws Throwable { StopWatch sw
- = new StopWatch(getClass().getSimpleName()); try { sw.start(pjp.getSignature().getName());
- return pjp.proceed(); } finally { sw.stop(); System.out.println(sw.prettyPrint());
- } } @Pointcut("execution(public com.shansun..(..))") public void profileMethod()
- {} }
- </span>
前文提到, 我们还需要一个 aop.xml. 这个文件要求放在 META-INF/aop.xml 路径下, 以告知 AspectJ Weaver 我们需要把 ProfilingAspect 织入到应用的哪些类中. mailto:br/>@Around(
- mailto:br/%3E@Around(
- <span style="font-family:'Courier New'; color: #008080; font-size: x-small;"><!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "mailto:br/>@Around( http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> http://www.eclipse.org/aspectj/dtd/aspectj.dtd ;
- <aspectj>
- <weaver>
- <include within="com.shansun..*" />
- </weaver>
- <aspects>
- <!-- weave in just this aspect -->
- <aspect name="com.shansun.multidemo.spring.ltw.ProfilingAspect" />
- </aspects>
- </aspectj></span>
目前为止, 本次切面的 "攻" 和 "受" 都准备好了, 我们还需要一个中间媒介 --LoadTimeWeaver. 我们将 Spring 的配置文件添加红色标识内容.
- <?xml version="1.0" encoding="GBK"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="Index of /schema/context" xmlns:tx="Index of /schema/tx"
- xsi:schemaLocation="
- Index of /schema/beans
- Index of /schema/aop
- Index of /schema/context
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">;
- <context:load-time-weaver aspectj-weaving="autodetect" />
- <context:component-scan base-package="com.shansun.multidemo"></context:component-scan>
- </beans>
通过 < context:load-time-weaveraspectj-weaving="on" /> 使 spring 开启 loadtimeweaver, 注意 aspectj-weaving 有三个选项: on, off, auto-detect, 如果设置为 auto-detect, spring 将会在 classpath 中查找 aspejct 需要的 META-INF/aop.xml, 如果找到则开启 aspectj weaving, 这个逻辑在 LoadTimeWeaverBeanDefinitionParser#isAspectJWeavingEnabled 方法中:
- <span style="font-family:'Courier New'; color: #7f0055; font-size: x-small;">
- <strong>
- protected boolean isAspectJWeavingEnabled(String value, ParserContext
- parserContext) { if ("on".equals(value)) { return true; } else if ("off".equals(value))
- { return false; } else { // Determine default... ClassLoader cl = parserContext.getReaderContext().getResourceLoader().getClassLoader();
- return (cl.getResource(ASPECTJ_AOP_XML_RESOURCE) != null); } }
- </strong>
- </span>
一切都准备就绪 -- 切面类, aop.xml,Spring 的配置, 我们就创建一个 main 方法来掩饰 LTW 的功效吧.
- public class Main {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- // DemoBean bean = (DemoBean) ctx.getBean("demoBean");
- DemoBean bean = new DemoBean();
- bean.run();
- }
- }
因为这个 LTW 使用成熟的 AspectJ, 我们并不局限于通知 Spring beans 的方法. 所以上述代码中从 ApplicationContext 中获取 Bean 和直接实例化一个 Bean 的效果是一样的.
注意, 这里以使用 Eclipse 演示上述代码为例, 需要在运行参数中稍作设置, 即添加前文提到的 - javaagent, 来取代默认的类加载器.
输出结果如下:
- Run
- StopWatch 'ProfilingAspect': running time (millis) = 0
- ms % Task name
- 0001 100% run
至此, LTW 可以正常使用了, 但是麻烦的是我需要在 VM 参数里加上 - javaagent 这么个东东, 如果不加会如何呢? 试试看.
- <span style="font-family:'Courier New'; color: #ff0000; font-size: x-small;">
- Exception in thread "main" java.lang.IllegalStateException: Must start
- with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
- at org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver.addTransformer(InstrumentationLoadTimeWeaver.java:88)
- at org.springframework.context.weaving.AspectJWeavingEnabler.postProcessBeanFactory(AspectJWeavingEnabler.java:69)
- at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:553)
- at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:536)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:362)
- at org.springframework.context.support.ClassPathXmlApplicationContext.
- <init>
- (ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.
- <init>
- (ClassPathXmlApplicationContext.java:83) at com.shansun.multidemo.spring.Main.main(Main.java:25)
- </span>
必需使用 java agent 么? 仔细观察异常的出处 InstrumentationLoadTimeWeaver. 再深入这个类的内容, 发现异常是由下述方法抛出的.
- public void addTransformer(ClassFileTransformer transformer) {
- Assert.notNull(transformer, "Transformer must not be null");
- FilteringClassFileTransformer actualTransformer =
- new FilteringClassFileTransformer(transformer, this.classLoader);
- synchronized (this.transformers) {
- if (this.instrumentation == null) {
- throw new IllegalStateException(
- "Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.");
- }
- this.instrumentation.addTransformer(actualTransformer);
- this.transformers.add(actualTransformer);
- }
- }
不难发现它在校验 instrumentation 是否为空的时候抛出的异常. 有办法啦, 重写 InstrumentationLoadTimeWeaver 的 addTransformer 方法, 隐匿异常即可.
- public class ExtInstrumentationLoadTimeWeaver extends
- InstrumentationLoadTimeWeaver {
- @Override
- public void addTransformer(ClassFileTransformer transformer) {
- try {
- super.addTransformer(transformer);
- } catch (Exception e) {
- }
- }
- }
这时, 我们还需要做一件事, 将 Spring 配置文件中的 load-time-weaver 入口设置为我们刚自定义的 ExtInstrumentationLoadTimeWeaver 即可.
- <?xml version="1.0" encoding="GBK"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="Index of /schema/aop"
- xmlns:context="Index of /schema/context" xmlns:tx="Index of /schema/tx"
- xsi:schemaLocation="
- Index of /schema/beans
- Index of /schema/aop
- Index of /schema/context
- Index of /schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">;
- <context:load-time-weaver weaver-class="com.shansun.multidemo.spring.ExtInstrumentationLoadTimeWeaver" aspectj-weaving="autodetect" />
- <context:component-scan base-package="com.shansun.multidemo"></context:component-scan>
- </beans>
再次运行我们的 main 方法, 发现只输出了如下结果, 切面没有起作用.
Run
看到了么, 同一份代码, 同一份配置, 只需要在 VM 启动参数中稍加变化, 即可实现同一个应用包在不同环境下可以自由选择使用使用 AOP 功能. 文章开头提到的那个需求也就迎刃而解了.
这只是一种解决途径, 相信大家会有更好的方案. 如果有, 请您告诉我. J
来源: http://blog.51cto.com/13917525/2311328