我是EJB3.1的新手。如果这是一个微不足道的问题,请耐心等待。我的要求是拥有一个单独的类,它将在不同的bean间共享某些数据。让不同的线程访问这个单例类的数据。让我试着用两个不同的类A和B来解释。@ EJB3中@ Singleton's bean的无状态bean访问数据
@Singleton
//@Local?? ,@Remote?? or @LocalBean ?
class A {
private List<CommonDTO> commonDTOList = new ArrayList<CommonDTO>();
.
. //other member variables, EJB beans which implement Remote interfaces.
.
init(){
//initialise commonDTOList here.
}
//getter
List<SomeDTO> getCommonDTOList(){
return commonDTOList;
}
}
@Stateless
Class B implements Interface { //Interface is @Remote
//need to access singleton Class A's getter , so that all the threads have the same commonDTOList.
@EJB
private A classA;
.
.//other member variables
.
@OverRide //overriding Interface2's method
public void doSomething(){
.
.//do some database transactions here , which can be done parallely by multiple threads, since this is stateless.
.
//now retrieve Class A'S CommonDTOList.
//This List should be shared across multiple threads of this stateless bean.
List<SomeDTO> someDTOListInsideStatelessBean = classA.getCommonDTOList();
}
}
的问题是什么注解我应该ClassA
这样我就可以在另一个无国籍豆访问其表? 我试过以下,但徒劳无功。
1)@Local
我不能使用,因为在上面。在classA
该成员变量具有@EJB
菜豆,实现@Remote
接口内嵌批注,提及等等。
2)@LocalBean
看起来是在这种情况下使用的。但是,一旦在ClassB
的方法“doSomething()
”内,classA
变量的所有 成员变量都为null。虽然List在启动期间已初始化。 我的印象是,自从它的单例以来,所有bean中将只有实例共享 。
3)@Remote
我不知道我是否应该在这里使用,但是也没有运气。
请大家帮忙。提前致谢。