gpt4 book ai didi

java - 向上转换时 AspectJ 和 Spring LTW 不起作用

转载 作者:行者123 更新时间:2023-11-30 09:40:26 24 4
gpt4 key购买 nike

我使用 AspectJ 设置了 LTW,并且 spring 非常快速且成功。这是设置:beans.xml:

<context:annotation-config />
<aop:aspectj-autoproxy />
<context:spring-configured />
<context:load-time-weaver />
<context:component-scan base-package="com.test.service" />

我的服务将自动连接到一个类:

@Service
public class MyService {
}

父类:

public class Bar {
}

可配置类, Autowiring 服务并扩展 Bar。

@Configurable
public class BarExtended extends Bar{
@Autowired
private MyService service;
public MyService getWeavedInObject(){
return service;
}
}

并且只是一个引用了父类 Bar 的类:

public class Foo {
private Bar bar;
public void setBar(Bar bar) {
this.bar = bar;
}
}

还有一个成功的测试用例。它只是创建一个 BarExtended 实例并检查 LTW 是否有效。 Foo 类什么都不做。

@Test
public void simple(){
Foo foo = new Foo();
BarExtended barExtended = new BarExtended();
assertNotNull("LTW didn't work.", barExtended.getWeavedInObject());
}

此测试运行绿色。但以下测试失败:

@Test
public void simple(){
Foo foo = new Foo();
BarExtended barExtended = new BarExtended();
foo.setBar(barExtended);
assertNotNull("LTW didn't work.", barExtended.getWeavedInObject());
}

我只是插入了将类 BarExtended 设置为 Foo 的行。沮丧使 AspjectJ 无法正常工作。

顺便说一句,当我将 Foo 类更改为使用 BarExtended 类时(因此不需要向上转型):

public class Foo {
private BarExtended bar;
public void setBar(BarExtended bar) {
this.bar = bar;
}
}

上面的测试会起作用。有谁知道为什么 AspjectJ 在向上转换可配置对象时表现如此奇怪?

编辑:Follwing 也失败了:

@Test
public void simple() {
Foo foo = new Foo();
BarExtended barExtended = new BarExtended();
Bar bar = (Bar) new BarExtended();
foo.setBar(bar);
assertNotNull("LTW didn't work.", barExtended.getWeavedInObject());
}

另一个 BarExtended 对象被设置为 Foo,第一个 barExtended 对象被 AspectJ 忽略。但是使用反射来实例化 BarExtended 作品:

@Test
public void simple() throws InstantiationException, IllegalAccessException{
Foo foo = new Foo();
Bar barExtended = (Bar) BarExtended.class.newInstance();
foo.setBar(barExtended);
assertNotNull("LTW didn't work.", ((BarExtended)barExtended).getWeavedInObject());
}

很奇怪,不是吗?

非常感谢

问候,

安德烈亚斯

最佳答案

我过去曾遇到过我认为 LTW 已配置的问题,但这并不是出于我不太确定的原因。因此,我现在 100% 明确地在我的配置中对您的配置文件进行以下更改,看看是否一切正常。

  <context:load-time-weaver aspectj-weaving="on" />

删除 <aop:aspectj-autoproxy />从您的配置来看,您不需要它,您已经真正运行了 LTW。

当您运行 JUnit 测试时,您是否传递了 vm 参数来告诉 JUnit LTW 代理在哪里?如果没有,那么您没有运行 LTW。

这是文档中关于 <context:load-time-weaver /> 的内容

Activates a Spring LoadTimeWeaver for this application context, available as a bean with the name "loadTimeWeaver". Any bean that implements the LoadTimeWeaverAware interface will then receive the LoadTimeWeaver reference automatically; for example, Spring's JPA bootstrap support. The default weaver is determined automatically. As of Spring 2.5: detecting Sun's GlassFish, Oracle's OC4J, Spring's VM agent and any ClassLoader supported by Spring's ReflectiveLoadTimeWeaver (for example, the TomcatInstrumentableClassLoader). The activation of AspectJ load-time weaving is specified via a simple flag (the 'aspectj-weaving' attribute), with the AspectJ class transformer registered through Spring's LoadTimeWeaver. AspectJ weaving will be activated by default if a "META-INF/aop.xml" resource is present in the classpath. This also activates the current application context for applying dependency injection to non-managed classes that are instantiated outside of the Spring bean factory (typically classes annotated with the @Configurable annotation). This will only happen if the AnnotationBeanConfigurerAspect is on the classpath (i.e. spring-aspects.jar), effectively activating "spring-configured" by default. See Javadoc for org.springframework.context.annotation.EnableLoadTimeWeaving for information on code-based alternatives to bootstrapping load-time weaving support.

所以总结一下 <context:load-time-weaver />似乎真的是关于定义一个 id 为 loadTimeWeaver 的 bean 并扫描类路径以查找特殊文件(如 aop.xml)以确定是否应打开 aspectJ。要确保 aspectJ 已打开,您确实需要设置 aspectj-weaving="on"这样,如果它由于某种原因无法打开 aspectJ,它将在启动时失败,这正是您想要的。在我的网络应用程序中,我有一个在网络应用程序启动时运行的测试,以确保 aspectJ 正在运行,如果没有运行,它会提示。

关于java - 向上转换时 AspectJ 和 Spring LTW 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9422200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com