Example usage for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext

List of usage examples for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext

Introduction

In this page you can find the example usage for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext.

Prototype

@Nullable
public static WebApplicationContext getWebApplicationContext(ServletContext sc) 

Source Link

Document

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener .

Usage

From source file:net.vitarara.groovyworks.GroovyStrutsSpringObjectFactory.java

@Inject
public void setServletContext(ServletContext servletContext) {
    log.info("Groovy Spring Plugin: Initializing");

    // Create the Groovy classloader and make it a child of the current thread's class loader.
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.setRecompileGroovySource(true);/*from w w w . jav a 2 s  . c  o m*/
    gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), cc);
    gcl.setShouldRecompile(true);
    // gcl = new GroovyClassLoader (Thread.currentThread().getContextClassLoader () );
    // gcl.setShouldRecompile (false);
    log.info("GroovyClassLoader recompilation set to: " + gcl.isShouldRecompile());

    log.info("Initializing Struts-Spring integration...");

    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (appContext == null) {
        // uh oh! looks like the lifecycle listener wasn't installed. Let's inform the user
        String message = "********** FATAL ERROR STARTING UP SPRING-STRUTS INTEGRATION **********\n"
                + "Looks like the Spring listener was not configured for your web app! \n"
                + "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\n"
                + "You might need to add the following to web.xml: \n" + "    <listener>\n"
                + "        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n"
                + "    </listener>";
        log.fatal(message);
        return;
    }

    this.setApplicationContext(appContext);

    int type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; // default
    if ("name".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
    } else if ("type".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
    } else if ("auto".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
    } else if ("constructor".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
    }
    this.setAutowireStrategy(type);

    this.setUseClassCache(useClassCache);

    log.info("... initialized Struts-Spring integration successfully");

    // Set the appContext's class loader to the GroovyClassLoader. The refresh the context.
    // ((org.springframework.core.io.DefaultResourceLoader) appContext).setClassLoader (gcl);
    // ((ConfigurableWebApplicationContext) appContext).refresh ();

    log.info("Groovy Spring Plugin: Initialization complete");
}

From source file:de.u808.simpleinquest.web.SimpleInquestServlet.java

private void testAndInitDB() {
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    UserManager userManager = (UserManager) context.getBean("userManager");
    User adminUser = userManager.fetchUser("Admin");
    if (adminUser == null) {
        adminUser = new User();
        adminUser.setUsername("Admin");
        adminUser.setPassword("Admin");
        userManager.makePersistent(adminUser);
    }/*from w w w  .j  a  v a2  s  .c  o  m*/
}

From source file:net.naijatek.myalumni.util.taglib.DisplayAboutUsTag.java

/**
 * Includes the body of the tag if the page attribute equals the value set
 * in the 'match' attribute./* www.  j  a  v  a2s  . c o  m*/
 *
 * @return SKIP_BODY if equalsAttribute body content does not equal the
 *   value of the match attribute, EVAL_BODY_include if it does
 * @throws JspException
 */
@Override
public final int doStartTag() throws JspException {
    request = (HttpServletRequest) pageContext.getRequest();
    WebApplicationContext wac = WebApplicationContextUtils
            .getWebApplicationContext(pageContext.getServletContext());
    systemConfigService = (ISystemConfigService) wac.getBean(BaseConstants.SERVICE_SYSTEM_CONFIG);
    return EVAL_BODY_BUFFERED;
}

From source file:com.github.mjeanroy.junit.servers.samples.jetty.webxml.IndexWithRunnerTest.java

@Test
public void it_should_have_an_index() {
    String url = url() + "index";
    String message = restTemplate.getForObject(url, String.class);
    assertThat(message).isNotEmpty().isEqualTo("Hello World");

    // Try to get servlet context
    ServletContext servletContext = jetty.getServletContext();
    assertThat(servletContext).isNotNull();

    // Try to retrieve spring webApplicationContext
    WebApplicationContext webApplicationContext = WebApplicationContextUtils
            .getWebApplicationContext(servletContext);
    assertThat(webApplicationContext).isNotNull();
}

From source file:architecture.ee.web.servlet.ViewRendererServlet.java

protected final void processRequest(HttpServletRequest httpservletrequest,
        HttpServletResponse httpservletresponse) throws ServletException, IOException {
    try {//  ww  w.  ja v  a 2 s  . com
        httpservletrequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                WebApplicationContextUtils.getWebApplicationContext(getServletContext()));
        renderView(httpservletrequest, httpservletresponse);
    } catch (ServletException servletexception) {
        throw servletexception;
    } catch (IOException ioexception) {
        throw ioexception;
    } catch (Exception exception) {
        throw new NestedServletException("View rendering failed", exception);
    }
}

From source file:com.vaadin.addon.jpacontainer.demo.servlet.SpringApplicationServlet.java

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    /*/*www .j  a v  a2  s . c o  m*/
     * Look up the name of the Vaadin application prototype bean.
     */
    applicationBean = servletConfig.getInitParameter("applicationBean");
    if (applicationBean == null) {
        if (logger.isErrorEnabled()) {
            logger.error("ApplicationBean not specified in servlet parameters");
        }
        throw new ServletException("ApplicationBean not specified in servlet parameters");
    }

    if (logger.isInfoEnabled()) {
        logger.info("Found Vaadin ApplicationBean [" + applicationBean + "]");
    }
    /*
     * Fetch the Spring web application context
     */
    applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());

    if (!applicationContext.isPrototype(applicationBean)) {
        if (logger.isWarnEnabled()) {
            logger.warn("ApplicationBean not configured as a prototype");
        }
    }

    applicationClass = (Class<? extends Application>) applicationContext.getType(applicationBean);
    if (logger.isDebugEnabled()) {
        logger.debug("Vaadin application class is [" + applicationClass + "]");
    }
}

From source file:com.github.mjeanroy.junit.servers.samples.tomcat.webxml.IndexWithRunnerTest.java

@Test
public void it_should_have_an_index() {
    String url = url() + "index";
    String message = restTemplate.getForObject(url, String.class);
    assertThat(message).isNotEmpty().isEqualTo("Hello World");

    // Try to get servlet context
    ServletContext servletContext = tomcat.getServletContext();
    assertThat(servletContext).isNotNull();

    // Try to retrieve spring webApplicationContext
    WebApplicationContext webApplicationContext = WebApplicationContextUtils
            .getWebApplicationContext(servletContext);
    assertThat(webApplicationContext).isNotNull();
}

From source file:com.ewcms.component.interaction.web.servlate.JavaScriptServlet.java

private InteractionServiceable getInteractionService() {
    ServletContext application = getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);
    return (InteractionServiceable) wac.getBean("interactionService");
}

From source file:org.apache.cxf.fediz.service.idp.MetadataServlet.java

public ApplicationContext getApplicationContext() {
    if (applicationContext == null) {
        LOG.debug(this.getServletContext().toString());
        applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    }/*from ww  w  .  jav a2  s . c  om*/
    return applicationContext;
}

From source file:dhbw.ka.mwi.businesshorizon2.SpringApplicationServlet.java

/**
 * Diese Methode wird einmal beim Initialisieren aufgerufen. Dort werden die Konfigurationseinstellungen
 * aus der web.xml gelesen. Darueberhinaus wird ein Spring-Application-Context aus der servletConfig
 * geholt, mit dem es moeglich ist, Beans mit Session-Scope zu erzeugen.
 * /*from w w  w.  ja  va  2 s. c om*/
 * @author Christian Gahlert
 */
@SuppressWarnings("unchecked")
@Override
public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);

    applicationBean = servletConfig.getInitParameter("applicationBean");

    if (applicationBean == null) {
        throw new ServletException("ApplicationBean not specified in servlet parameters");
    }

    applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    applicationClass = (Class<? extends Application>) applicationContext.getType(applicationBean);
}