为什么 Spring 的 @Configurable 的 AspectJ 编译时编织不起作用? [英] Why doesn't AspectJ compile-time weaving of Spring's @Configurable work?

查看:26
本文介绍了为什么 Spring 的 @Configurable 的 AspectJ 编译时编织不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 5: 我已经下载了基于最新 Eclipse 的最新 Spring ToolsSuite IDE.当我将我的项目作为 Maven 项目导入时,Eclipse/STS 似乎使用 Maven 目标来构建我的项目.这意味着 AspectJ 终于可以在 Eclipse 中正常工作了.

Update 5: I've downloaded the latest Spring ToolsSuite IDE based on the latest Eclipse. When I import my project as a Maven project, Eclipse/STS appears to use the Maven goals for building my project. This means AspectJ finally works correctly in Eclipse.

更新 4:我最终只使用 Maven + AspectJ 插件进行编译时编织,有效地绕过了 Eclipse 的机制.

Update 4: I have ended up just using Maven + AspectJ plugin for compile-time weaving, effectively bypassing Eclipse's mechanism.

更新 3: 似乎 AspectJ 的 Eclipse 插件破坏了 Eclipse 正确发布到 Tomcat 的能力.只有删除项目上的 AspectJ 功能,我才能让它再次正确发布.很烦人.

Update 3: It seems AspectJ's Eclipse plug-in breaks Eclipse's ability to correctly Publish to Tomcat. Only by removing the AspectJ capability on a project can I get it to properly Publish again. Very annoying.

更新 2: 我现在在 Eclipse 中使用它.这么说让我很不舒服,但我不知道我是如何从 Eclipse 或 Maven 构建中得到它的.这似乎是一个编译问题而不是运行时问题.

Update 2: I have this now working in Eclipse. It makes me very uncomfortable to say this, but I have no idea how I got it working from either Eclipse or Maven builds. It appears to be a compile issue rather than a run-time issue.

更新 1: 看来我已经通过 Maven 构建使其工作了,但我不知道如何.Eclipse 仍然不起作用.我在 pom.xml 中唯一改变的是添加这些(无关紧要的?)配置参数:

Update 1: It appears I've gotten this to work via Maven builds, but I have no idea how. Eclipse still doesn't work. The only thing I changed in the pom.xml was adding these (insignificant?) configuration parameters:

<source>1.6</source>
<complianceLevel>1.6</complianceLevel>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>

我其实很担心我会重复这个问题,一切都不一致.随着我了解更多信息,我会不断更新这个问题.

I'm actually worried that I have a repeat of this problem, where everything works inconsistently. I will keep this question updated as I learn more.

关于 Eclipse,我通过采用我希望编织的二进制方面取得了一些进展 - 在本例中为 spring-aspects.jar - 并将其从我的类路径中复制出来.然后我将这个现在外部 jar 添加到我的方面路径.这样做之后,Eclipse 在我的代码中正确地显示了 AspectJ 标记.令人讨厌的是,我不能将 spring-aspects.jar 留在我的 Java 构建路径 中,该路径由 Maven 通过 Maven 插件为我维护.然而,出于某种原因,AspectJ 插件不会看到二进制方面,除非它们被明确添加到方面路径.

With regards to Eclipse, I made some progress by taking the binary aspects I wish to weave - in this case spring-aspects.jar - and copying it out of my classpath. I then add this now external jar to my Aspect Path. After doing this, Eclipse properly shows me AspectJ markers in my code. It's annoying that I can't just leave spring-aspects.jar in my Java Build Path which is maintained by Maven for me via the Maven plug-in. For some reason, however, the AspectJ plug-in doesn't see the binary aspects unless they're explicitly added to the Aspect Path.

原帖:@Configurable 是一个 Spring 注释,它允许将依赖项注入到 Spring 外部实例化的对象中(例如,通过 Hibernate 或某些 Factory 类).

Original Post: @Configurable is a Spring annotation that allows dependencies to be injected into objects instantiated external to Spring (for example, by Hibernate or some Factory class).

我之前在加载时编织中使用了此注释,并且大部分都有效.有时我会启动,但不会注入任何内容.这个问题引发了这个 StackOverflow 问题.答案不多,但大多数人建议我尝试编译时编织,因为可靠性更高.

I was using this annotation previously with load-time weaving and it mostly worked. Occasionally I would boot up and nothing would get injected. This issue spawned this StackOverflow question. There weren't many answers, but most suggested that I try compile-time weaving instead due to greater reliability.

我为 Eclipse 和 Maven 安装了 AspectJ 插件.这两者都产生了似乎是正确编译的类.在 AspectJ 编译之前,我在文本编辑器中打开了其中一个类,但没有发现对 AspectJ 的引用.我在 AspectJ 编译后打开它,Eclipse 和 Maven 生成的版本都有对 org.aspectj.weaver.MethodDeclarationLineNumber 的引用.这就是为什么我认为它被正确编译的原因.问题是一旦部署,就不会注入任何依赖项.

I installed the AspectJ plug-in for Eclipse and Maven. Both of these produce what appears to be properly compiled classes. I've opened up one of the classes in a text editor before AspectJ compilation and found no references to AspectJ. I opened it up after AspectJ compilation and both Eclipse and Maven generated versions have a reference to org.aspectj.weaver.MethodDeclarationLineNumber. This is why I assume it's being properly compiled. The problem is that once deployed, no dependencies get injected.

我的 Spring applicationContext.xml 确实包括以下内容:

My Spring applicationContext.xml does include the following:

    <context:spring-configured />

    <context:component-scan base-package="com.myapp" />

以上是标记为@Configurable 的类完成 DI 所需的全部内容吗?在从加载时编织到编译时编织的转换过程中,我从我的 applicationContext.xml,以及来自我的 context.xml 的 Spring Tomcat weaver.

Is the above all that's needed for classes marked @Configurable to have DI done? During the conversion from load-time weaving to compile-time weaving, I removed META-INF/aop.xml, <context:load-time-weaver /> from my applicationContext.xml, and Spring's Tomcat weaver from my context.xml.

我该如何进一步调查这个问题?可能的原因是什么?

How can I investigate this problem further? What are possible causes?

推荐答案

它适用于我们使用编译时编织的 maven,尝试添加以下插件:

It works for us on maven using compile time weaving, try adding the following plugins:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
    <compilerVersion>1.6</compilerVersion>
    <fork>true</fork>
    <source>1.6</source>
    <target>1.6</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
    <execution>
        <id>compile</id>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <verbose>false</verbose>
            <outxml>true</outxml>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
            </aspectLibraries>
        </configuration>
        <goals>
            <goal>compile</goal>
        </goals>
    </execution>
    <execution>
        <id>test-compile</id>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <verbose>false</verbose>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
            </aspectLibraries>
        </configuration>
        <goals>
            <goal>test-compile</goal>
        </goals>
    </execution>
</executions>
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.4</version>
    </dependency>
</dependencies>
</plugin>

它作为两个单独的执行步骤完成,允许您为单元测试和编译添加不同的方面库.

Its done as two separate execution steps to allow you to add different aspect libraries for unit testing and compilation.

您还需要为 spring-aspects 库添加以下依赖项:

You'll also need the following dependency added for the spring-aspects library:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <scope>compile</scope>
    </dependency>

这篇关于为什么 Spring 的 @Configurable 的 AspectJ 编译时编织不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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