在 Spring LTW 环境中使用 Maven 运行单元测试

发布于 2024-12-14 01:30:00 字数 13285 浏览 0 评论 0原文

我正在使用 Spring LoadTimeWeaving 功能在 ddd 架构中开发一个应用程序。问题是我可以使用 eclipse 运行 Junit 测试,但不能通过 Maven 运行。我已经尝试了各个网站上提供的所有选项,但它根本不起作用。我收到以下异常:

由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为“loadTimeWeaver”的 bean 时出错:bean 初始化失败;嵌套异常是 java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] 不提供 'addTransformer(ClassFileTransformer)' 方法。指定自定义 LoadTimeWeaver 或使用 Spring 代理启动 Java 虚拟机: -javaagent:org.springframework.instrument.jar

我已将仪器 jar 添加为 argLine 的一部分到 Surefire 插件。以下是我的代码工件。请让我知道我哪里出了问题或者是否有其他解决方案。

我正在使用 Eclipse Indigo 进行开发,Mac OSX 上使用 Maven 3.0

pom.xml(仅针对重要细节进行缩写)

<dependencies>
    <!-- Spring Dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>


    <!-- Spring Security Dependencies -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring.security-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.security-version}</version>
    </dependency>

    <!-- AspectJ 
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>-->

    <!-- Persistence Dependencies -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.18</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.java-persistence</groupId>
        <artifactId>jpa-api</artifactId>
        <version>2.0-cr-1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.genericdao</groupId>
        <artifactId>dao</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.genericdao</groupId>
        <artifactId>search-jpa-hibernate</artifactId>
        <version>1.0.0</version>
    </dependency>

    <!-- Web Dependencies -->
    <dependency>
        <groupId>opensymphony</groupId>
        <artifactId>sitemesh</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- Testing Dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument</artifactId>
        <version>3.1.0.M2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>jtemplate</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <reportPlugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                        <version>2.2</version>
                        <reports>
                            <report>index</report>
                            <report>project-team</report>
                            <report>license</report>
                            <report>mailing-list</report>
                            <report>dependencies</report>
                            <report>dependency-convergence</report>
                            <report>plugin-management</report>
                            <report>cim</report>
                            <report>issue-tracking</report>
                            <report>scm</report>
                            <report>summary</report>
                        </reports>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-report-plugin</artifactId>
                        <version>2.10</version>
                        <configuration>
                            <forkMode>pertest</forkMode>
                            <argLine>-javaagent:/Users/vishals/.m2/repository/org/springframework/spring-instrument/3.1.0.M2/spring-instrument-3.1.0.M2.jar</argLine>
                            <useSystemClassloader>true</useSystemClassloader>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.7</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-checkstyle-plugin</artifactId>
                        <version>2.6</version>
                        <configuration>
                            <configLocation>${basedir}/src/main/resources/mkcl-egov.xml</configLocation>
                        </configuration>
                        <!-- <configuration> Adopt config location to your needs, or remove 
                            configuration entry completly to use default version. <configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation> 
                            </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> 
                            <artifactId>findbugs-maven-plugin</artifactId> <version>2.3.1</version> <configuration> 
                            <argLine>-Xmx256m</argLine> <argLine>-XX:MaxPermSize=256m</argLine> </configuration> -->
                        <!-- Usually findbugs needs a lot of memory, change these values if 
                            needed. -->
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>jdepend-maven-plugin</artifactId>
                        <version>2.0-beta-2</version>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>cobertura-maven-plugin</artifactId>
                        <version>2.4</version>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>taglist-maven-plugin</artifactId>
                        <version>2.4</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jxr-plugin</artifactId>
                        <version>2.1</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-pmd-plugin</artifactId>
                        <version>2.5</version>
                        <configuration>
                            <linkXref>true</linkXref>
                            <minimumTokens>100</minimumTokens>
                            <minimumPriority>3</minimumPriority>
                            <!-- Change minimum priority to see more or less messages -->
                            <targetJdk>1.6</targetJdk>
                        </configuration>
                    </plugin>
                </reportPlugins>
            </configuration>
        </plugin>
    </plugins>
</build>

springContext.xml(其具有以下用于启用 LTW 功能的标签)

<context:load-time-weaver/>

I am developing an application in a ddd architecture using Spring LoadTimeWeaving feature. The problem is i can run my Junit tests using eclipse but not through Maven. I have tried all the options given on various sites but it simply isn't working. I get the following exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar

I have added the instrument jar as a part of argLine to surefire plugin. Following are my code artifacts. Please let me know where i am going wrong or if there is any other solution.

I am using Eclipse Indigo for Development, Maven 3.0 on Mac OSX

pom.xml (shortened for important details only)

<dependencies>
    <!-- Spring Dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>


    <!-- Spring Security Dependencies -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring.security-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.security-version}</version>
    </dependency>

    <!-- AspectJ 
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>-->

    <!-- Persistence Dependencies -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.18</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.java-persistence</groupId>
        <artifactId>jpa-api</artifactId>
        <version>2.0-cr-1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.genericdao</groupId>
        <artifactId>dao</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.genericdao</groupId>
        <artifactId>search-jpa-hibernate</artifactId>
        <version>1.0.0</version>
    </dependency>

    <!-- Web Dependencies -->
    <dependency>
        <groupId>opensymphony</groupId>
        <artifactId>sitemesh</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- Testing Dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument</artifactId>
        <version>3.1.0.M2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>jtemplate</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <reportPlugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                        <version>2.2</version>
                        <reports>
                            <report>index</report>
                            <report>project-team</report>
                            <report>license</report>
                            <report>mailing-list</report>
                            <report>dependencies</report>
                            <report>dependency-convergence</report>
                            <report>plugin-management</report>
                            <report>cim</report>
                            <report>issue-tracking</report>
                            <report>scm</report>
                            <report>summary</report>
                        </reports>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-report-plugin</artifactId>
                        <version>2.10</version>
                        <configuration>
                            <forkMode>pertest</forkMode>
                            <argLine>-javaagent:/Users/vishals/.m2/repository/org/springframework/spring-instrument/3.1.0.M2/spring-instrument-3.1.0.M2.jar</argLine>
                            <useSystemClassloader>true</useSystemClassloader>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.7</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-checkstyle-plugin</artifactId>
                        <version>2.6</version>
                        <configuration>
                            <configLocation>${basedir}/src/main/resources/mkcl-egov.xml</configLocation>
                        </configuration>
                        <!-- <configuration> Adopt config location to your needs, or remove 
                            configuration entry completly to use default version. <configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation> 
                            </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> 
                            <artifactId>findbugs-maven-plugin</artifactId> <version>2.3.1</version> <configuration> 
                            <argLine>-Xmx256m</argLine> <argLine>-XX:MaxPermSize=256m</argLine> </configuration> -->
                        <!-- Usually findbugs needs a lot of memory, change these values if 
                            needed. -->
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>jdepend-maven-plugin</artifactId>
                        <version>2.0-beta-2</version>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>cobertura-maven-plugin</artifactId>
                        <version>2.4</version>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>taglist-maven-plugin</artifactId>
                        <version>2.4</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jxr-plugin</artifactId>
                        <version>2.1</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-pmd-plugin</artifactId>
                        <version>2.5</version>
                        <configuration>
                            <linkXref>true</linkXref>
                            <minimumTokens>100</minimumTokens>
                            <minimumPriority>3</minimumPriority>
                            <!-- Change minimum priority to see more or less messages -->
                            <targetJdk>1.6</targetJdk>
                        </configuration>
                    </plugin>
                </reportPlugins>
            </configuration>
        </plugin>
    </plugins>
</build>

springContext.xml (Its has the following tag for enabling LTW feature)

<context:load-time-weaver/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2024-12-21 01:30:00

您需要包含一个 spring-instrument.jar 代理,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4</version>
    <configuration>
            <forkMode>once</forkMode>
            <argLine>
                 -javaagent:"${settings.localRepository}/org/springframework/spring-instrument/${spring.framework.version}/spring-instrument-${spring.framework.version}.jar"
        </argLine>
            <useSystemClassloader>true</useSystemClassloader>
    </configuration>
</plugin>

(将 ${spring.framework.version} 和 ${settings.localRepository} 替换为您的值)

以及当然,AspecJ Maven 插件如下:

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>aspectj-maven-plugin</artifactId>
     <version>1.3</version>
     <executions>
         <execution>
             <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>                     
             <goals>
                 <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                 <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
             </goals>
         </execution>
    </executions>
</plugin>

要使 AspectJ 正常工作,您还需要添加 aspectjrt 依赖项:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>${aspectjrt.version}</version>
</dependency>

这里有一个关于如何使用 Maven 运行 AspectJ 测试

you need to include a spring-instrument.jar agent as:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4</version>
    <configuration>
            <forkMode>once</forkMode>
            <argLine>
                 -javaagent:"${settings.localRepository}/org/springframework/spring-instrument/${spring.framework.version}/spring-instrument-${spring.framework.version}.jar"
        </argLine>
            <useSystemClassloader>true</useSystemClassloader>
    </configuration>
</plugin>

( replace ${spring.framework.version} and ${settings.localRepository} with your values )

and of course an AspecJ Maven plugin as:

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>aspectj-maven-plugin</artifactId>
     <version>1.3</version>
     <executions>
         <execution>
             <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>                     
             <goals>
                 <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                 <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
             </goals>
         </execution>
    </executions>
</plugin>

For AspectJ to work, you would also need to add an aspectjrt dependency:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>${aspectjrt.version}</version>
</dependency>

Here a full example on how to run AspectJ tests with Maven

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文