Created
October 11, 2013 05:47
-
-
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
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
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