问题
I'm building little framework for data processing with EJB 3.
I have Entity Access Object tier which abstracts from data source. Now I need some kind of factory which will give me right bean to query entities.
Is it safe to pass looked up through JNDI local bean interfaces as parameters to another local beans? Will each method invocation from this local interface be addressed to the same bean or each call will be passed to different stateless beans as in @EJB
occasion?
回答1:
You don't have any guarantee that using JNDI you will give you the same EJB instance, so it's the same as with dependency injection using @EJB
or @Inject
. The only difference between @EJB
and JNDI lookup is the SFSB. In this case, the container is required to provide you new SFSB instance every time you use JNDI lookup.
However, in my opinion, in EJB 3.x and dependency injection era, it's more easy to understand the @EJB
/@Inject
annotation. No need to pass any object references, just define your dependencies (EJB collaborators) in each EJB using @EJB
.
来源:https://stackoverflow.com/questions/8616285/ejb3-how-jndi-lookup-works