我想为EJB添加依赖项.我怎么用Spring做到这一点?从属对象是一般服务对象.根据下面的代码,我想连接myDependency而不必使用’new’.
EJB在weblogic中运行.
@Stateless(mappedName = "MyBean")
public class MyBean implements MyBeanRemote, MyBeanLocal {
@EJB(name = "MyOtherBean")
private MyOtherBean myOtherBean;
private MyDependency myDependency;
...
}
解决方法:
这在Spring documentation中有详细描述:
For EJB 3 Session Beans and Message-Driven Beans, Spring provides a
convenient interceptor that resolves Spring 2.5’s @Autowired
annotation in the EJB component class:
org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.
This interceptor can be applied through an @Interceptors annotation in
the EJB component class, or through an interceptor-binding XML element
in the EJB deployment descriptor.
@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MyFacadeEJB implements MyFacadeLocal {
// automatically injected with a matching Spring bean
@Autowired
private MyComponent myComp;
// for business method, delegate to POJO service impl.
public String myFacadeMethod(...) {
return myComp.myMethod(...);
}
...
}
然而,无状态EJB和Spring bean提供了或多或少相同的可能性.将它们混合在一起似乎不必要的复杂性.