Created
October 21, 2010 04:53
-
-
Save mojavelinux/637959 to your computer and use it in GitHub Desktop.
A bridge between the ServletContext life cycle events and CDI observers (with example observer ApplicationInitializer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// uncomment line if you want the instance to be retained in application scope | |
// @ApplicationScoped | |
public class ApplicationInitializer | |
{ | |
public void onStartup(@Observes @Initialized ServletContext ctx) | |
{ | |
System.out.println("Initialized web application at context path " + ctx.getContextPath()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Qualifier | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.FIELD, ElementType.PARAMETER}) | |
public @interface Destroyed {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Qualifier | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.FIELD, ElementType.PARAMETER}) | |
public @interface Initialized {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebListener | |
public class ServletContextLifecycleNotifier implements ServletContextListener | |
{ | |
@Inject @Any | |
private Event<ServletContext> servletContextEvent; | |
@Override | |
public void contextInitialized(ServletContextEvent sce) | |
{ | |
servletContextEvent.select(new AnnotationLiteral<Initialized>() {}).fire(sce.getServletContext()); | |
} | |
@Override | |
public void contextDestroyed(ServletContextEvent sce) | |
{ | |
servletContextEvent.select(new AnnotationLiteral<Destroyed>() {}).fire(sce.getServletContext()); | |
} | |
} |
Very nice DanTT, thanks for this!
This was a really helpful solution to an issue I was trying to solve without using additional frameworks like Solder. Thanks Dan.
Very nice example. Thanks.
Hello,
the example does not work in WAS Liberty profile if I uncommon ApplicationScoped. I get:
org.apache.webbeans.event.ObserverMethodImpl I Cannot send event to bean in non-active context
probably because ApplicationScope is not yet initialised by the time the ServletContextListener. contextInitialized() method is invoked. Any workarounds? Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great... i searched the whole web for a possibility to get the ServletContext inside my CDI app inside a bean... Thank you very much!!!