【问题标题】:Compile time weaving for DI in non-spring managed classes非弹簧管理类中 DI 的编译时间编织
【发布时间】:2013-07-26 18:42:08
【问题描述】:

我想为标有@Configurable 注释的类配置编译时编织,以便能够将spring 依赖项注入使用new 运算符的类。我不想使用加载时编织,因为我无权运行应用程序服务器的脚本,所以我无法修改它。另外我希望能够在测试中使用这些类,我的意思是从 IDE 运行测试用例。我只找到了关于 web 和 spring 参考上的加载时间编织的信息,而没有关于编译时间编织的配置的信息。

PS。我在 Maven 中使用 spring

【问题讨论】:

  • Eclipse 或 IntelliJ 或其他?

标签: spring maven compile-time-weaving


【解决方案1】:

所以另一个答案也是有效的,但我想我会更详细地介绍这种方法的一些含义。

我使用的设置基本是这样的:-

      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
            <configuration>
                <outxmlfile>META-INF/aop.xml</outxmlfile>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <source>1.7</source>
                <target>1.7</target>
                 <forceAjcCompile>true</forceAjcCompile>
            </configuration>
        </plugin>

现在,关于依赖项的一些附加信息:-

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${dep.spring}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${dep.spring}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

我想你错过了用于编织的持久性 api。

编辑:与春季的https://jira.springsource.org/browse/SPR-6819 错误有关。这似乎就是您需要持久性 API 的原因。

如果类在 ide 中未编织(这对我来说经常发生),创建一个 maven 作业来编织也很有帮助。

aspectj:compile

最后,如果你打算对你的类进行单元测试,在这个阶段之后编织类会很有用。我们在prepare-package 阶段进行编织。如果您想这样做,请添加

      <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>

到您的插件。

我希望这会有所帮助,因为要让这种方法在 IDE 中发挥出色可能会很棘手。

【讨论】:

  • 为什么需要持久化 api?我不使用它。我无法想象它如何用于编织。我假设 spring-aspects 中的某些方面与此 api 一起使用,因此 aspectj-maven-pluging 必须有一个选项来从 aspectjLibraries 中选择具体方面以用于编织过程。另外,为什么我需要aspectjrt依赖,我不使用加载时编织
  • 嗨,我为持久性添加了 jira 问题。
  • 此外,方面运行时不耦合到编译时编织。您可以通过避免使用 cflow 切入点指示符和 thisJoinPoint 操作(我已阅读)来最小化它的使用需求。您是否处于无法向项目或其他东西引入更多依赖项的情况?
  • 也许你知道它什么时候会修好?是的,我不能引入更多的依赖项。
  • 你用的是什么版本的spring? 3.2.1.RELEASE 似乎有一个修复(虽然错误仍然是开放的:S)
【解决方案2】:

在你的 pom 中这样的东西应该可以工作......

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <configuration>
                            <source>${source}</source>
                            <target>${target}</target>
                            <verbose>true</verbose>
                            <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>${source}</source>
                            <target>${target}</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>
                    <!-- Ensure aspectj tools version used by compiler is the same version used as dependency. Avoids warning -->
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

然后确保你的类路径上有 spring-aspects。

对于 Eclipse 中的方面 - 安装 AJDT

【讨论】:

  • 我在类路径上有 spring-aspects,但是当我尝试编译(mvn compile)项目时,我得到了嵌套错误:can't determine annotations of missing type javax.persistence.Entity。但是我什至没有在我的项目中使用它(我既没有hibernate也没有其他jpa兼容工具)
  • 我还注意到它试图编织我所有的类(即使它们没有被标记为`@Configurable')
  • 另外,${aspectj.version} 是什么。它意味着什么版本?
  • 我只是使用一个属性来指定我的 aspectj 版本。所以我认为它是1.7.2。我也知道无法确定缺少类型 javax.persistence.Entity 消息的注释。不要认为这很重要。是的,它至少需要扫描所有内容,以防它可能包含它感兴趣的注释。
  • 你的意思是aspectj或aspectjrt下的aspectjweaver版本吗?也许有一些方法可以告诉 aspectj-maven-plugin 跳过一些包、类?
猜你喜欢
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多