code_junkie

JSF initialize application-scope bean when context initialized

Nov 12th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <listener>
  2. <listener-class>appobj.MyApplicationContextListener</listener-class>
  3. </listener>
  4.  
  5. public class MyApplicationContextListener implements ServletContextListener {
  6.  
  7. private static final String FOO = "foo";
  8.  
  9. public void contextInitialized(ServletContextEvent event) {
  10. MyObject myObject = new MyObject();
  11. event.getServletContext().setAttribute(FOO, myObject);
  12. }
  13.  
  14. public void contextDestroyed(ServletContextEvent event) {
  15. MyObject myObject = (MyObject) event.getServletContext().getAttribute(
  16. FOO);
  17. try {
  18. event.getServletContext().removeAttribute(FOO);
  19. } finally {
  20. myObject.dispose();
  21. }
  22. }
  23.  
  24. }
  25.  
  26. <f:view>
  27. <h:outputText value="#{applicationScope.foo.value}" />
  28. <h:outputText value="#{foo.value}" />
  29. </f:view>
  30.  
  31. FacesContext.getCurrentInstance()
  32. .getExternalContext().getApplicationMap().get("foo");
Add Comment
Please, Sign In to add comment