在不在我的项目中的类上加载时间编织
我正在尝试使用 Spring 为我创建的项目中的几个类提供加载时间编织.当我调用一个不在我本地项目中的类时,我无法让它工作.
I'm trying to use Spring to provide load time weaving to several classes in a project I've created. I'm having trouble getting it to work when I call a class that is not within my local project.
我创建了一个名为 ExampleClass
的类,当我对其中的一个方法执行 @Around
时,我可以看到我对返回值所做的修改,但是当我尝试对 String
做同样的事情,我无法得到任何结果.
I created a class named ExampleClass
and when I perform an @Around
on a method in there, I can see the modifications I made to the return, however when I attempt to do the same to String
I'm unable to get any results.
这是我的方面
代码:
@Pointcut("call(* java.lang.String.*(..))")
public void cutString() {}
@Before("cutString()")
public void aroundString() throws Throwable {
System.out.println("I Never See This");
}
这是我对该代码的调用:
Here's my call to that code:
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:my-context.xml");
String string = new String("I Only See This");
System.out.println(string.toLowerCase());
}
}
my-context.xml
的内容只是一个
.
我定义了一个 aop.xml
,正如我所说,它适用于一个类,但不适用于另一个类:
I have an aop.xml
defined which as I said works for one class, but not another:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<include within="com.example.*" />
<include within="java.lang.String" />
</weaver>
<aspects>
<aspect name="com.example.PerformMonitor" />
</aspects>
</aspectj>
我是否遗漏了什么,或者这是 Spring 和 AspectJ 的限制?
Am I missing something, or is this a limitation of Spring and AspectJ?
默认 aspectj 不会编织任何 Java 标准类.据我所知,这是防止安全泄漏的限制.它在 aspectj 文档中有所描述.可能有一个属性允许更改此行为,但在开始执行此操作之前,您应该非常确定您需要它.
Per default aspectj will not weave any java standard classes. This is a limitation to prevent security leaks as far as i remember. It is described in the aspectj documentation. Probably there is a property which allows changing this behavior but you should be very sure you need this before you start doing that.
只需用您自己的一类创建另一个罐子,然后尝试编织这个罐子.这应该是开箱即用的.
Just create another jar with one class of your own and try to weave this one. This should work out of the box.