在我的项目中,我目前在编译时使用AspectJ(由于某些限制而不仅仅是Spring AOP).为了加速Eclipse的开发,我想在加载时进行编织.我成功地做到了这一点,但有一个主要的约束:为我的服务使用包含一些事务方法的接口.如果我使用其实现而不是其接口声明服务,则在调用者类中,没有编织,因此不支持任何事务.
因此,如果AspectJ支持它,如何在没有接口的情况下使用加载时编织配置AspectJ?
我创建了一个重现问题的小项目:
以下测试失败.
以下测试成功:
注入的服务是通过其接口而不是其实现来声明的(即通过"@Inject MyService service"替换"@Inject MyServiceImpl服务"),测试成功.
在编译期间执行编织(在这种情况下,配置,POM和Spring应用程序上下文明显不同).但我的目标是在加载时进行编织,以避免每次保存Java文件时的编织阶段.
Spring AOP(tx:annotation-driven mode ="proxy"),即基于代理的解决方案,用于代替AspectJ.但在这种情况下,我们遇到了自调用问题,即目标对象中调用目标对象的其他方法的方法,即使被调用的方法用@Transactional标记,也不会在运行时导致实际的事务.
AspectJ的LTW/SRC /测试/ JAVA/myCompany的/ aspectj_ltw/MyServiceImplTest.java
package mycompany.aspectj_ltw;
import static junit.framework.Assert.assertTrue;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
public class MyServiceImplTest {
@Inject
MyServiceImpl service;
@Test
public void shouldBeExecutedInTransaction() {
assertTrue(this.service.isExecutedInTransaction());
}
}
Run Code Online (Sandbox Code Playgroud)
AspectJ的LTW/SRC /主/爪哇/ myCompany的/ aspectj_ltw/MyService.java
package mycompany.aspectj_ltw;
public interface MyService {
boolean isExecutedInTransaction();
}
Run Code Online (Sandbox Code Playgroud)
AspectJ的LTW/SRC /主/爪哇/ myCompany的/ aspectj_ltw/MyServiceImpl.java
package mycompany.aspectj_ltw;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; …Run Code Online (Sandbox Code Playgroud)