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

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

Introduction

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

Prototype

public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
        throws IllegalStateException 

Source Link

Document

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

Usage

From source file:org.molasdin.wbase.jsf.spring.web.SpringBeansRewireListener.java

@Override
public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
    HttpSession session = httpSessionEvent.getSession();
    WebApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(session.getServletContext());
    AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();
    for (String entry : IteratorUtils.asIterable(IteratorUtils.asIterator(session.getAttributeNames()))) {
        if (bf.containsBean(entry)) {
            Object bean = session.getAttribute(entry);
            bean = bf.configureBean(bean, entry);
            session.setAttribute(entry, bean);
        }//from  ww w .  j  a  v a  2  s.  com
    }

}

From source file:gov.nih.nci.ncicb.cadsr.umlmodelbrowser.struts.actions.BaseAction.java

public void setServlet(ActionServlet actionServlet) {
    super.setServlet(actionServlet);
    ServletContext servletContext = actionServlet.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

}

From source file:com.acc.filter.FilterSpringUtil.java

/**
 * The same as {@link #getSpringBean(HttpServletRequest, String, Class)} but uses ServletContext as the first
 * parameter. It might be used in places, where HttpServletRequest is not available, but ServletContext is.
 */// w w  w. j  a v  a  2 s  .c  o m
public static <T> T getSpringBean(final ServletContext servletContext, final String beanName,
        final Class<T> beanClass) {
    T ret = null;
    final WebApplicationContext appContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);

    if (StringUtils.isNotBlank(beanName)) {
        try {
            ret = (T) appContext.getBean(beanName);
        } catch (final NoSuchBeanDefinitionException nsbde) {
            LOG.warn("No bean found with the specified name. Trying to resolve bean using type...");
        }
    }
    if (ret == null) {
        if (beanClass == null) {
            LOG.warn("No bean could be resolved. Reason: No type specified.");
        } else {
            final Map<String, T> beansOfType = appContext.getBeansOfType(beanClass);
            if (beansOfType != null && !beansOfType.isEmpty()) {
                if (beansOfType.size() > 1) {
                    LOG.warn("More than one matching bean found of type " + beanClass.getSimpleName()
                            + ". Returning the first one found.");
                }
                ret = beansOfType.values().iterator().next();
            }
        }
    }
    return ret;
}

From source file:com.sbu.controller.Feed_Form_Employer_Edit_Contorller.java

@Override
public void init() throws ServletException {
    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    employerService = context.getBean(EmployerManager.class);
    projectService = context.getBean(ProjectManager.class);
    skillsService = context.getBean(SkillsManager.class);
}

From source file:gov.nih.nci.cabig.ctms.web.filters.ContextRetainingFilterAdapter.java

protected WebApplicationContext getApplicationContext() {
    return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
}

From source file:com.exxonmobile.ace.hybris.storefront.servlets.util.FilterSpringUtil.java

/**
 * The same as {@link #getSpringBean(HttpServletRequest, String, Class)} but uses ServletContext as the first
 * parameter. It might be used in places, where HttpServletRequest is not available, but ServletContext is.
 *///  w w  w  .  j  a va  2  s.c  om
public static <T> T getSpringBean(final ServletContext servletContext, final String beanName,
        final Class<T> beanClass) {
    T ret = null;
    final WebApplicationContext appContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);

    if (StringUtils.isNotBlank(beanName)) {
        try {
            ret = (T) appContext.getBean(beanName);
        } catch (final NoSuchBeanDefinitionException ex) {
            LOG.warn("No bean found with the specified name. Trying to resolve bean using type...");
        }
    }
    if (ret == null) {
        if (beanClass == null) {
            LOG.warn("No bean could be resolved. Reason: No type specified.");
        } else {
            final Map<String, T> beansOfType = appContext.getBeansOfType(beanClass);
            if (beansOfType != null && !beansOfType.isEmpty()) {
                if (beansOfType.size() > 1) {
                    LOG.warn("More than one matching bean found of type " + beanClass.getSimpleName()
                            + ". Returning the first one found.");
                }
                ret = beansOfType.values().iterator().next();
            }
        }
    }
    return ret;
}

From source file:sk.stefan.remserver.serviceImpl.MsgSenderImpl.java

/**
 * Instead of @Autowired/ extends SpringBeanAutowiringSupport which doesn't work.
 *//*from  w  w w .ja va2 s . c  o m*/
private void wireBeans() {

    ServletContext servletContext;
    servletContext = (ServletContext) context.getMessageContext().get("javax.xml.ws.servlet.context");
    WebApplicationContext webApplicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);
    producer = (Producer) webApplicationContext.getAutowireCapableBeanFactory().getBean("producer");

}

From source file:ca.flop.jpublish.SpringActionSupport.java

/**
 * @param context the JPublish context/*from  w  ww. j a va 2  s . c o  m*/
 * @return a Spring application context
 */
protected final ApplicationContext getApplicationContext(JPublishContext context) {
    ServletContext sc = context.getApplication();
    return WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
}

From source file:net.sourceforge.vulcan.web.VulcanContextListener.java

public void contextInitialized(ServletContextEvent event) {
    final ServletContext context = event.getServletContext();

    wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    stateManager = (StateManager) wac.getBean(Keys.STATE_MANAGER, StateManager.class);

    context.setAttribute(Keys.STATE_MANAGER, stateManager);
    context.setAttribute(Keys.EVENT_POOL, wac.getBean(Keys.EVENT_POOL));
    context.setAttribute(Keys.BUILD_OUTCOME_STORE, wac.getBean(Keys.BUILD_OUTCOME_STORE));

    JstlFunctions.setWebApplicationContext(wac);
    XslHelper.setWebApplicationContext(wac);

    try {//from w w  w .j  a  va  2  s  .c  o m
        stateManager.start();
    } catch (Exception e) {
        final EventHandler eventHandler = (EventHandler) wac.getBean(Keys.EVENT_HANDLER, EventHandler.class);

        eventHandler
                .reportEvent(new ErrorEvent(this, "errors.load.failure", new String[] { e.getMessage() }, e));
    }
}

From source file:sk.stefan.remserver.serviceImpl.MsgDelivererImpl.java

/**
 * Instead of @Autowired/ extends SpringBeanAutowiringSupport which doesn't work.
 */// ww w.j  ava2s. com
private void wireBeans() {

    ServletContext servletContext;
    servletContext = (ServletContext) context.getMessageContext().get("javax.xml.ws.servlet.context");
    WebApplicationContext webApplicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);
    consumer = (Consumer) webApplicationContext.getAutowireCapableBeanFactory().getBean("consumer");

}