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

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

问题描述

我正在尝试使用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 的内容只是一个< context:load-time-weaver/> .

我定义了一个 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.

只需使用您自己的一个类创建另一个jar,然后尝试编织它即可.这应该开箱即用.

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天全站免登陆