我有一个耳朵,它包含一个 ejb 和一个 war 模块,以及其他本地 jar 依赖项,例如 dao 和服务。
我已将项目放在 github https://github.com/SunPj/spring-load-time-weaving
我使用弹簧加载时间编织。我的 dao 应用程序上下文包括以下几行
<tx:annotation-driven mode="aspectj"/>
<context:load-time-weaver aspectj-weaving="on"/>
我的缓存上下文有<cache:annotation-driven mode="aspectj"/>
我的网络模块有一个控制器
package ru.test.web;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ru.test.service.TestService;
import java.util.Map;
@Controller
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/userlist")
public String userList(Map<String, Object> model) {
String st = ArrayUtils.toString(testService.getUsers());
model.put("text", st);
return "main";
}
@RequestMapping("/remove-user")
public String removeUser(@RequestParam("userId") int userId, Map<String, Object> model) {
testService.removeUser(userId);
model.put("text", "Removed userid = "+userId);
return "main";
}
@RequestMapping("/remove-user-ex")
public String removeUserEx(@RequestParam("userId") int userId, Map<String, Object> model) {
try {
testService.removeUserWithException(userId);
} catch (RuntimeException e){
model.put("text", "Exception = "+e.getMessage());
}
return "main";
}
@RequestMapping("/random")
public String random(Map<String, Object> model) {
String s = new String();
for (int i = 0; i< 100; i++){
s = s + +testService.getRandom()+", ";
}
model.put("text", "Random = "+s);
return "main";
}
@RequestMapping({"/", "/home"})
public String index(Map<String, Object> model) {
model.put("text", "Home page!");
return "main";
}
@RequestMapping("/user-cache-evict")
public String userCacheEvict(Map<String, Object> model) {
testService.userCacheEvict();
model.put("text", "user cache evicted!");
return "main";
}
@RequestMapping("/data-cache-evict")
public String dataCacheEvict(Map<String, Object> model) {
testService.dataCacheEvict();
model.put("text", "data cache evicted!");
return "main";
}
}
服务看起来像
@Service
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
private TestDao testDao;
@Override
public String getUsers() {
return testDao.showUsers() + "( real count = "+testDao.getRealUsersCount()+")";
}
@Override
public void removeUser(int userId) {
testDao.removeUser(userId);
}
@Override
public void removeUserWithException(int userId) {
testDao.removeUser(userId);
throw new RuntimeException("ololo");
}
@Override
public int getRandom() {
return testDao.getRandom();
}
@Override
public void userCacheEvict() {
testDao.userCacheEvict();
}
@Override
public void dataCacheEvict() {
testDao.dataCacheEvict();
}
@Override
public int showUsers() {
return 1;
}
}
道
@Repository
public class TestDaoImpl extends AbstractDao implements TestDao {
@Override
public void removeUser(int id) {
getJdbcTemplate().execute("DELETE FROM TABLE1 WHERE id = " + id);
}
@Override
public String showUsers() {
String users = ArrayUtils.toString(getJdbcTemplate().queryForList("SELECT name FROM TABLE1", String.class));
return users + ":" + getCachedUsersCount();
}
@Override
@Cacheable(value = "User")
public int getCachedUsersCount(){
int cnt = getJdbcTemplate().queryForInt("SELECT count(*)name FROM TABLE1");
return cnt;
}
@Override
public int getRealUsersCount(){
int cnt = getJdbcTemplate().queryForInt("SELECT count(*)name FROM TABLE1");
return cnt;
}
@Override
@CacheEvict(value = "User", allEntries = true)
public void userCacheEvict(){
}
@Override
@CacheEvict(value = "PermanentData", allEntries = true)
public void dataCacheEvict(){
}
@Override
@Cacheable(value = "PermanentData")
public int getRandom() {
return new Random().nextInt();
}
}
当 ejb 没有服务依赖时,一切正常。一旦我将服务依赖项添加到 ejb 模块(某些 ejb 的 pom),事务就会停止工作。
在 github 上的最后 2 次提交(提交 973f48a9e0db11edc3675fd09b6930f05b98afc9 和提交 e7d280cb93b2303bfcfbd5cd0b55d24b002be8fc)显示了工作和没有工作的情况。