@PostConstruct
메서드가 자체 내에서 @Asynchronous
메서드를 호출해야하는 singleton bean이 있습니다. this
을 사용하면 직접 호출 할 수 없기 때문에 직접 호출 할 수는 없습니다. 원형이기 때문에 나는 @Inject
자체가 될 수 없다.싱글 톤 Java EE Bean은 어떻게 자체에 대한 참조를 얻을 수 있습니까?
0
A
답변
4
당신은 래퍼의 이러한 유형을 사용할 수 있습니다 :
@Singleton
public class SingletonBean {
@Stateless
public static class AsynchronousMethodLauncher{
@EJB
private SingletonBean singletonBean;
public void launch(){
singletonBean.asynchronousMethod();
}
}
@EJB
AsynchronousMethodLauncher launcher;
@Asynchronous
public void asynchronousMethod(){
//Place your code here
}
public void yourMethod(){
launcher.launch();
}
}
3
나는 자연 자바 EE 방법 제안
:
@Singleton
public class AsyncSingletonBeanBean {
@Resource
private SessionContext sessionContext;
@PostConstruct
public void init() {
AsyncSingletonBeanBean myBean = sessionContext.getBusinessObject(this.getClass());
myBean.foo();
}
@Asynchronous
public Future<String> foo() {
return new AsyncResult<String>("Hello");
}
}
좋은 제안을! (더 좋은 대답이 게시되면 받아 들일 것입니다.) – necromancer
다른 대답이 더 좋을 때, 받아 들여야합니다;) –