Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample CDI extension for eagerly instantiating @ApplicationScoped beans annotated @startup
@ApplicationScoped
@Startup
public class StartupBean
{
@PostConstruct
public void onStartup()
{
System.out.println("Application starting up.");
}
}
public class StartupBeanExtension implements Extension
{
private final Set<Bean<?>> startupBeans = new LinkedHashSet<Bean<?>>();
<X> void processBean(@Observes ProcessBean<X> event)
{
if (event.getAnnotated().isAnnotationPresent(Startup.class) &&
event.getAnnotated().isAnnotationPresent(ApplicationScoped.class))
{
startupBeans.add(event.getBean());
}
}
void afterDeploymentValidation(@Observes AfterDeploymentValidation event, BeanManager manager)
{
for (Bean<?> bean : startupBeans)
{
// the call to toString() is a cheat to force the bean to be initialized
manager.getReference(bean, bean.getBeanClass(), manager.createCreationalContext(bean)).toString();
}
}
}
@itfobos

This comment has been minimized.

Copy link

@itfobos itfobos commented May 22, 2013

Hello,

Do we need to add any files to META-INF/services dirrectory?
Do u have an example of using this approach?

I'm trying to make extension like u and get no success. Using Glassfish 3.1.21

@oloftus

This comment has been minimized.

Copy link

@oloftus oloftus commented Jun 2, 2013

According to http://ovaraksin.blogspot.co.uk/2013/02/eager-cdi-beans.html you do...

"The extensions should be registered in a file META-INF/services/javax.enterprise.inject.spi.Extension. The file has only one line with a fully qualified path to the EagerExtension class, e.g. mydomain.mypackage.EagerExtension."

Though I haven't tried implementing this yet

@odrotbohm

This comment has been minimized.

Copy link

@odrotbohm odrotbohm commented Jan 12, 2014

Has this ever worked? .toString() just returns as call from Object and there doesn't seem to be a provider agnostic way to enforce the actual instance to be created.

@JalalSordo

This comment has been minimized.

Copy link

@JalalSordo JalalSordo commented Jul 16, 2016

have anyone ever tested this ?

@cray935

This comment has been minimized.

Copy link

@cray935 cray935 commented Jul 22, 2016

hi, i'm just researching and according to the spec of "ProcessBean" this won't work...
Why the posted solution may not work is that "ProcessBean" is the event right before a bean gets deployed. the call of Object.toString() is because the guy enabled bean discovery mode "all" what lifts all classpath classes up to beans.

EDIT: but you can use the event to gather information about the beans discovered... usually you might be interested in "ManagedBean"s

@sermojohn

This comment has been minimized.

Copy link

@sermojohn sermojohn commented Nov 4, 2016

You can consider using the @initialized qualifier introduced in CDI 1.1 spec, and provided by the Weld implementation.
Here is a demo code: https://gist.github.com/sermojohn/c1044df560dbd86e4b9fae0283c64265

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment