我的耳朵包含一个ejb和一个war模块,以及其他本地jar依赖项,例如dao和services。
我已将项目放在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";
}
}
Servise看起来像
@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模块添加了服务依赖(some-ejb的pom),事务就停止了工作。
在github上最后2次提交(提交973f48a9e0db11edc3675fd09b6930f05b98afc9并提交e7d280cb93b2303bfcfbd5cd0b55d24b002be8fc)显示工作和无工作情况。
答案 0 :(得分:0)
Spring Framework有自己的spring容器,它在内部管理所有事务,并且它提供依赖注入和IoC,因此不需要EJB。你也可以将你的EJB逻辑放在spring中,而不需要部署EJB。 / p>
这就是为什么你只需要在applicationContext.xml中添加以下标记:并扫描包。