我有一個生產者方法的Singleton EJB。無論如何鎖定@Produces方法
@javax.ejb.Singleton
public class MyBean{
private Something something;
@Produces
public MySomething getSomething() {
if(null == something){
LOG.info("Initializing MySomething.");
something = new Something();
}
return something;
}
}
我以爲這會鎖,但我看到這個"Initializing MySomething."
在日誌中多次東西,然後拋出一個java.lang.StackOverflowError
。
所以看起來像我需要鎖定這個@Produces
方法。
爲此可以使用java.util.concurrent.Semaphore
嗎?
我相信這是我想要的。我會試一試。 – DarVar