我在 Jboss 和 EJB 3.0 的冒险中挣扎得更远。 我加入了带有 EJB 的 Spring 3.0。我有 3 个模块——带有 Spring Controller 的 web 模块、带有我的 EJB bean 的 ejb 模块和带有其他类(如 dao 或一些助手)的 spring mogule。尽在耳边。
因此,在我的 Spring Controller 中,我有以下代码:
@Controller
public class IndexController {
@EJB
PaymentRemote paymentRemote;
@RequestMapping(value = "/index")
public ModelAndView index() throws Exception {
ModelAndView mav = new ModelAndView("index/index");
paymentRemote.savePayment(123, "bla222");
paymentRemote.sayGoodbye();
return mav;
}
在我的 EJB 模块中,我有以下有状态 bean:
@Stateful
@Interceptors( SpringBeanAutowiringInterceptor.class)
public class PaymentRemoteImpl implements PaymentRemote{
@Autowired
ExampleService exampleService;
public void savePayment(long payment, String name) throws Exception {
exampleService.savePayment(123, "kk");
}
@Remove
public void sayGoodbye() {
System.out.println("I want to finish my task here!");
}
}
每个依赖项都已正确注入(inject)。当我用无状态 bean 测试这段代码时,它工作得很好。当谈到有状态的 bean 时,当我调用我的方法 sayGoodbye() 时,我不能再次调用这个 bean。我得到异常:
javax.ejb.NoSuchEJBException: Could not find stateful bean: a74a2l-n1u5tx-gcwd0e6a-1-gcwd18fo-9h
我不明白这种情况:/我要求容器移除 bean,但后来我想再次使用它,但它想再次为我找到同一个 bean。我认为虽然我要求删除它,但它会根据我的要求再次创建。 谁能帮我解决我的问题?我卡住了。
最佳答案
您不能将有状态 session Bean (SFSB) 注入(inject)到可能由多个并发客户端(如 Servlet 或 Controller )共享的多线程组件中。相反,执行 JNDI 查找并将 bean 放入 HttpSession
。
另见
关于java - "javax.ejb.NoSuchEJBException: Could not find stateful bean: ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3489368/