- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!