Skip to content

Instantly share code, notes, and snippets.

@schakko
Created October 11, 2013 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schakko/6930116 to your computer and use it in GitHub Desktop.
Save schakko/6930116 to your computer and use it in GitHub Desktop.
JSF @ManagedBean; will be loaded on web application startup and initializes the Spring application context
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.XmlWebApplicationContext;
import de.ckl.ejb.SpringContextBean;
/**
* Application wide settings. The instance of this bean will be loaded on
* application startup.
*
* @author ckl
*/
@ApplicationScoped
@ManagedBean(name = "app", eager = true)
public class ApplicationBean {
Logger log = LoggerFactory.getLogger(ApplicationBean.class);
@EJB
SpringContextBean springContextBean;
/**
* On application startup, this method initializes the default
* {@link XmlWebApplicationContext} and injects the context into the
* {@link SpringContextBean}.
*/
@PostConstruct
public void onApplicationStartup() {
log.info("Initializing default Spring XmlWebApplicationContext");
ServletContext sc = (ServletContext) FacesContext.getCurrentInstance()
.getExternalContext().getContext();
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setServletContext(sc);
ContextLoader contextLoader = new ContextLoader(ctx);
contextLoader.initWebApplicationContext(sc);
log.info("Spring XmlWebApplicationContext initialized");
springContextBean.setApplicationContext(ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment