0

我正在创建一个使用 aspectj 事务的新项目。它还使用包含服务的遗留 jar,这些服务在需要接口的情况下使用代理方法。

我正在使用 java config 并且当我设置

@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)

然后我从遗留库访问代理样式服务时抛出以下异常:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

如果我改为:

@EnableTransactionManagement(mode=AdviceMode.PROXY)

然后我没有得到问题,但是我不能在我的新项目中使用 aspectj 样式的事务。

我尝试@EnableTransactionManagement为每个adviceMode 添加两个注释,但这是不允许的。

这是带注释的类

@EnableWebMvc
@Configuration
@ComponentScan("com.mydomain")
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
public class ApplicationConfig  extends WebMvcConfigurerAdapter {
...

我还在遗留项目中添加了 aspectj maven 插件,希望它能够在编译时处理编织,因此 aspectj 事务可以工作。但这并没有解决问题。

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

是否可以让 spring 处理两种建议模式?我该怎么做?

或者有没有其他方法可以解决这个问题。

4

1 回答 1

0

问题出在遗留项目上的 aspectj 配置上。

当我运行 mvn compile 时,它​​变得很明显。我必须添加依赖项:

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

使用 maven 编译时它可以工作,但我仍然无法在 eclipse 中工作。我必须右键单击 Eclipse 中的遗留项目:

Configure>Convert to Aspectj Project

然后我可以从 Eclipse 进行部署,并且我在遗留 jar 中获得了 aspectj 事务支持。

于 2015-11-27T11:15:13.237 回答