在不在我的项目中的类上加载时间编织 [英] Load Time Weaving on classes not within my project

查看:17
本文介绍了在不在我的项目中的类上加载时间编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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.

这篇关于在不在我的项目中的类上加载时间编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆