There shouldn’t be any difference, but there is no need to to have @ApplicationScoped on
both the producer method and the managed bean you declare it on, and do lazy init. Putting
it on the producer method means CDI will cache it. Putting it on the declaring bean means
the same bean will be called each time to execute the method. With the code you show, the
simplest thing is to just put @Produces @ApplicationScoped on the create() method.
As a side note, you probably want to use @ApplicationScoped not @Singleton.
On 9 Sep 2014, at 08:08, Ondrej Zizka <ozizka(a)redhat.com> wrote:
Hi, with this
----------------------------------------------------------------
@Singleton
public class GraphContextFactoryImpl implements GraphContextFactory
{
@Inject
private GraphApiCompositeClassLoaderProvider graphApiCompositeClassLoaderProvider;
@Inject
private Imported<Service<? extends VertexFrame>> graphServices;
@Inject
private GraphTypeRegistry graphTypeRegistry;
private GraphContext graphContext;
----------------------------------------------------------------
is there a difference between the following two?
Basically I just moved the core of the creation.
A is master, B is my branch, results in the GraphContext being created at each @Inject.
I think I am missing something here.
Thanks, Ondra
---A-------------------------------------------------------------
@Override
public GraphContext create(){
return produceGraphContext();
}
@Produces @ApplicationScoped
public GraphContext produceGraphContext(){
if (this.graphContext == null){
this.graphContext = new GraphContextImpl(graphServices,
this.graphTypeRegistry, this.graphApiCompositeClassLoaderProvider);
}
return graphContext;
}
----B-----------------------------------------------------------
@Override
public GraphContext create() {
return new GraphContextImpl(
this.graphServices,
this.graphTypeRegistry,
this.graphApiCompositeClassLoaderProvider);
}
@Produces @ApplicationScoped
public GraphContext produceGraphContext(){
if (this.graphContext == null) {
this.graphContext = this.create();
}
return graphContext;
}