【问题标题】:Spring @Configurable annotation with AspectJ带有 AspectJ 的 Spring @Configurable 注解
【发布时间】:2017-02-15 04:11:31
【问题描述】:

无法使用Aspectj 编译我的项目。 Apache CXF 存在一个问题,ResourceContext.getResource(SomeClass.class) 创建了一个简单的对象,而不是 Spring 管理的对象。所以我想用编织和@Configurable来渡过这个难关。我让它在我的测试 Spring Boot 应用程序中工作(如果需要,我可以在 Github 上提供一个链接),使用@Configurable 本身和@EnableSpringConfigured 进行以下设置:

这是我的 pom.xml 的快照(Spring 版本是 4.3.3.RELEASE):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>

aspectj-maven-plugin 插件配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.8</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <complianceLevel>1.8</complianceLevel>
        <showWeaveInfo>true</showWeaveInfo>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

但是,当我尝试在我公司的实际项目中应用上述配置时,我收到了这个奇怪的错误:

[ERROR] *path to the java file being weaving* can't determine annotations of missing type javax.transaction.Transactional
[ERROR] when weaving type *the full java class name*
[ERROR] when weaving classes
[ERROR] when weaving
[ERROR] when batch building BuildConfig[null] #Files=27 AopXmls=#0
[ERROR] [Xlint:cantFindType]
[ERROR] error at (no source information available)

我的测试项目没有使用@Transactional,但真正的项目使用了。所以我尝试添加 spring-txpersistence-api 依赖项,但没有任何效果。最后一点:该项目在我第二次运行mvn install 时构建成功,但每次运行mvn clean install 时均不成功。

非常感谢任何帮助,因为我真的被这个错误困住了。

【问题讨论】:

    标签: spring maven spring-aop spring-transactions compile-time-weaving


    【解决方案1】:

    将以下依赖项添加到类路径应该可以解决问题:

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>javax.transaction-api</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    

    【讨论】:

    • 谢谢,斯塔斯!真正解决了当前的问题。现在我可以构建项目了,但不幸的是,我发现一些 Configurable 类也使用了 Transactional。它在运行时导致以下错误:Caused by: java.lang.IllegalStateException: Post-processor tried to replace bean instance of type *The original class name* with (proxy) object of type *Some weird weaved proxy class name* - not supported for aspect-configured classes!
    • @snoopy-whoopy 这是一个不同的问题,所以为它打开一个新问题。如果您找到并回答了,请也发布您的答案!
    • @cb4 感谢您的建议!我找到了答案,但我是新手,所以我应该回答例如作为常规答案还是对我的问题描述的评论?
    • @snoopy-whoopy 您应该创建一个全新的问题,然后发布您的答案。这样更干净,并允许其他人搜索该特定问题。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多