Example usage for org.springframework.web.context WebApplicationContext getBeansOfType

List of usage examples for org.springframework.web.context WebApplicationContext getBeansOfType

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getBeansOfType.

Prototype

<T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException;

Source Link

Document

Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

Usage

From source file:grails.util.GrailsWebUtil.java

/**
 * Binds a Mock implementation of a GrailsWebRequest object to the current thread. The mock version uses
 * instances of the Spring MockHttpServletRequest, MockHttpServletResponse and MockServletContext classes.
 *
 * @param ctx The WebApplicationContext to use
 * @param request The request//from   w  ww. j ava 2  s .co  m
 * @param response The response
 *
 * @see org.springframework.mock.web.MockHttpServletRequest
 * @see org.springframework.mock.web.MockHttpServletResponse
 * @see org.springframework.mock.web.MockServletContext
 *
 * @return The GrailsWebRequest instance
 */
public static GrailsWebRequest bindMockWebRequest(WebApplicationContext ctx, MockHttpServletRequest request,
        MockHttpServletResponse response) {
    GrailsWebRequest webRequest = new GrailsWebRequest(request, response, ctx.getServletContext(), ctx);
    request.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest);
    for (ParameterCreationListener listener : ctx.getBeansOfType(ParameterCreationListener.class).values()) {
        webRequest.addParameterListener(listener);
    }
    RequestContextHolder.setRequestAttributes(webRequest);
    return webRequest;
}

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.
 *//*ww w. j av  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 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: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.
 *///ww  w . j  a  va 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:org.xaloon.wicket.component.plugin.PluginManager.java

@SuppressWarnings("unchecked")
public void init(WebApplicationContext context) {
    Collection<Plugin> items = context.getBeansOfType(Plugin.class).values();
    for (Plugin entry : items) {
        register(entry);/* ww w .  ja v  a 2 s .  com*/
    }
}

From source file:org.xaloon.wicket.component.mounting.PageAnnotationScannerContainer.java

@SuppressWarnings("unchecked")
public void scan(AbstractWebApplication application, WebApplicationContext context) {
    scan(application, "org.xaloon.wicket.component", ".wspx");
    Collection<PageScanner> items = context.getBeansOfType(PageScanner.class).values();
    for (PageScanner entry : items) {
        scan(application, entry.getPackageName(), entry.getSuffix());
    }// w  ww .j a  v  a  2s .  c o  m
}

From source file:org.xaloon.wicket.component.mounting.PageAnnotationScannerContainer.java

@SuppressWarnings("unchecked")
public void scan(WebApplication application, WebApplicationContext context) {
    scan(application, "org.xaloon.wicket.component", ".wspx");
    Collection<PageScanner> items = context.getBeansOfType(PageScanner.class).values();
    for (PageScanner entry : items) {
        scan(application, entry.getPackageName(), entry.getSuffix());
    }//from  w w w  . j ava  2s .  c o  m

}

From source file:net.jawr.web.bundle.processor.spring.SpringControllerBundleProcessor.java

/**
 * Initialize the servlets which will handle the request to the JawrSpringController 
 * @param servletContext the servlet context
 * @return the list of servlet definition for the JawrSpringControllers
 * @throws ServletException if a servlet exception occurs
 */// ww  w.j  a v a 2  s.com
@SuppressWarnings("rawtypes")
public List<ServletDefinition> initJawrSpringServlets(ServletContext servletContext) throws ServletException {

    List<ServletDefinition> jawrServletDefinitions = new ArrayList<ServletDefinition>();
    ContextLoader contextLoader = new ContextLoader();
    WebApplicationContext applicationCtx = contextLoader.initWebApplicationContext(servletContext);
    Map<?, ?> jawrControllersMap = applicationCtx.getBeansOfType(JawrSpringController.class);

    Iterator<?> entrySetIterator = jawrControllersMap.entrySet().iterator();
    while (entrySetIterator.hasNext()) {

        JawrSpringController jawrController = (JawrSpringController) ((Map.Entry) entrySetIterator.next())
                .getValue();
        Map<String, Object> initParams = new HashMap<String, Object>();
        initParams.putAll(jawrController.getInitParams());
        ServletConfig servletConfig = new MockServletConfig(SPRING_DISPATCHER_SERVLET, servletContext,
                initParams);
        MockJawrSpringServlet servlet = new MockJawrSpringServlet(jawrController, servletConfig);
        ServletDefinition servletDefinition = new ServletDefinition(servlet, servletConfig);
        jawrServletDefinitions.add(servletDefinition);
    }

    contextLoader.closeWebApplicationContext(servletContext);
    return jawrServletDefinitions;
}

From source file:sk.openhouse.web.filter.LocaleConfigurerFilter.java

@Override
protected void initFilterBean() throws ServletException {

    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());

    Map resolvers = context.getBeansOfType(LocaleResolver.class);
    if (resolvers.size() == 1) {
        localeResolver = (LocaleResolver) resolvers.values().iterator().next();
    }//from  ww  w.ja va 2 s  .  c o m
}

From source file:io.lavagna.web.security.SecurityFilter.java

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    WebApplicationContext ctx = getRequiredWebApplicationContext(filterConfig.getServletContext());
    for (Entry<String, SecurityConfiguration> kv : ctx.getBeansOfType(SecurityConfiguration.class).entrySet()) {
        pathsToCheck.put(kv.getKey(), ImmutablePair.of(kv.getValue(), kv.getValue().buildMatcherList()));
    }//  w  ww .  j a v  a2 s  .  c o m
}

From source file:org.parancoe.plugin.configuration.InitializerContextListener.java

@Override
public void contextInitialized(ServletContextEvent evt) {
    log.info("parancoe-plugin-configuration contextInitialized");
    WebApplicationContext webApplicationContext = WebApplicationContextUtils
            .getWebApplicationContext(evt.getServletContext());
    Map<String, ConfigurationCollection> configurationCollections = webApplicationContext
            .getBeansOfType(ConfigurationCollection.class);
    log.info("Found " + configurationCollections.size() + " configuration collections.");
    for (Map.Entry<String, ConfigurationCollection> entry : configurationCollections.entrySet()) {
        log.info("Loading " + entry.getKey() + " configuration collection...");
        ConfigurationCollection value = entry.getValue();
        configurationService.initializeConfiguration(value);
    }/*from   w  ww.  java2 s .c o m*/
}