Example usage for org.springframework.context ApplicationContext getBeansOfType

List of usage examples for org.springframework.context ApplicationContext getBeansOfType

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext 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:org.appcomponents.platform.mvc.PlatformRequestMappingHandlerMapping.java

protected HandlerExecutionChain handle(HttpServletRequest request, Component component) throws Exception {
    ApplicationContext applicationContext = component.getApplicationContext();
    if (this.applicationContext.equals(applicationContext)) {
        return null;
    } else {/* www .  jav a  2  s.  c o m*/
        Map<String, HandlerMapping> handlerMappingMap = applicationContext.getBeansOfType(HandlerMapping.class);
        for (HandlerMapping hm : handlerMappingMap.values()) {
            if (hm.getClass().equals(getClass())) {
                continue;
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Testing handler map [" + hm + "] in PlatformRequestMappingHandlerMapping");
            }
            HandlerExecutionChain handler = hm.getHandler(request);
            if (handler != null) {
                return handler;
            }
        }
        return null;
    }
}

From source file:edu.internet2.middleware.shibboleth.common.config.security.CertPathPKIXValidationOptionsTest.java

/**
 * Test configuring basic options with default values.
 * /*from  ww w  . j  a va  2s  .c  om*/
 * @throws Exception thrown if there is a problem
 */
public void testDefaultValues() throws Exception {
    ApplicationContext appContext = createSpringContext(new String[] { DATA_PATH + "/config/base-config.xml",
            DATA_PATH + "/config/security/CertPathPKIXValidationOptions1.xml", });

    CertPathPKIXValidationOptions pkixOptions = (CertPathPKIXValidationOptions) appContext
            .getBeansOfType(CertPathPKIXValidationOptions.class).values().iterator().next();
    assertNotNull(pkixOptions);

    assertEquals(true, pkixOptions.isProcessEmptyCRLs());
    assertEquals(true, pkixOptions.isProcessExpiredCRLs());
    assertEquals(true, pkixOptions.isProcessCredentialCRLs());
    assertEquals(new Integer(1), pkixOptions.getDefaultVerificationDepth());
    assertEquals(false, pkixOptions.isForceRevocationEnabled());
    assertEquals(true, pkixOptions.isRevocationEnabled());
}

From source file:edu.internet2.middleware.shibboleth.common.config.security.CertPathPKIXValidationOptionsTest.java

/**
 * Test configuring basic options with non-default values.
 * /*from   www.j a  v a 2  s.  c om*/
 * @throws Exception thrown if there is a problem
 */
public void testNonDefaultValues() throws Exception {
    ApplicationContext appContext = createSpringContext(new String[] { DATA_PATH + "/config/base-config.xml",
            DATA_PATH + "/config/security/CertPathPKIXValidationOptions2.xml", });

    CertPathPKIXValidationOptions pkixOptions = (CertPathPKIXValidationOptions) appContext
            .getBeansOfType(CertPathPKIXValidationOptions.class).values().iterator().next();
    assertNotNull(pkixOptions);

    assertEquals(false, pkixOptions.isProcessEmptyCRLs());
    assertEquals(false, pkixOptions.isProcessExpiredCRLs());
    assertEquals(false, pkixOptions.isProcessCredentialCRLs());
    assertEquals(new Integer(3), pkixOptions.getDefaultVerificationDepth());
    assertEquals(true, pkixOptions.isForceRevocationEnabled());
    assertEquals(false, pkixOptions.isRevocationEnabled());
}

From source file:com.avanza.astrix.spring.AstrixFrameworkBean.java

private DynamicConfig getDynamicConfig(ApplicationContext applicationContext) {
    Collection<DynamicConfig> dynamicConfigs = applicationContext.getBeansOfType(DynamicConfig.class).values();
    if (dynamicConfigs.isEmpty()) {
        return null;
    }//  w  ww .  ja v a  2 s.c o m
    if (dynamicConfigs.size() == 1) {
        return dynamicConfigs.iterator().next();
    }
    throw new IllegalArgumentException("Multiple DynamicConfig instances found in ApplicationContext");
}

From source file:org.xmatthew.spy2servers.component.web.ComponentsViewServlet.java

private ComponentContext getComponentContext() {
    ApplicationContext context = ContextServiceLocator.getContext();
    Map beansMap = context.getBeansOfType(CoreComponent.class);
    if (beansMap != null && beansMap.size() > 0) {
        return ((CoreComponent) beansMap.values().iterator().next()).getContext();
    }//from   w ww  .ja va 2s.c om
    return null;
}

From source file:com.kixeye.chassis.support.ChassisConfiguration.java

/**
 * Initializes the health check registry
 *
 * @return health check registry bean/*w w w .  ja  v a 2s . c o  m*/
 */
@Bean
public HealthCheckRegistry healthCheckRegistry(ApplicationContext context, DiscoveryManager eureka) {
    final HealthCheckRegistry bean = new HealthCheckRegistry();

    // auto-register beans implementing health checks
    Map<String, HealthCheck> healthChecks = context.getBeansOfType(HealthCheck.class);
    for (HealthCheck check : healthChecks.values()) {
        bean.register(check.getClass().getName(), check);
    }

    // connect health checks into Eureka
    if (!disableEureka) {
        eureka.getDiscoveryClient().registerHealthCheckCallback(new HealthCheckCallback() {
            @Override
            public boolean isHealthy() {
                for (Entry<String, HealthCheck.Result> entry : bean.runHealthChecks().entrySet()) {
                    if (!entry.getValue().isHealthy()) {
                        return false;
                    }
                }
                return true;
            }
        });
    }

    return bean;
}

From source file:org.ldaptive.servlets.AbstractServletSearchTemplatesExecutor.java

@Override
public void initialize(final ServletConfig config) {
    final String springContextPath = config.getInitParameter(SPRING_CONTEXT_PATH) != null
            ? config.getInitParameter(SPRING_CONTEXT_PATH)
            : DEFAULT_SPRING_CONTEXT_PATH;
    logger.debug("{} = {}", SPRING_CONTEXT_PATH, springContextPath);

    final ApplicationContext context = new ClassPathXmlApplicationContext(springContextPath);
    setSearchExecutor(context.getBean(AggregatePooledSearchExecutor.class));
    logger.debug("searchExecutor = {}", getSearchExecutor());

    final Map<String, PooledConnectionFactory> factories = context
            .getBeansOfType(PooledConnectionFactory.class);
    setConnectionFactories(factories.values().toArray(new PooledConnectionFactory[factories.size()]));
    logger.debug("connectionFactories = {}", Arrays.toString(getConnectionFactories()));

    final Map<String, SearchTemplates> templates = context.getBeansOfType(SearchTemplates.class);
    setSearchTemplates(templates.values().toArray(new SearchTemplates[templates.size()]));
    logger.debug("searchTemplates = {}", Arrays.toString(getSearchTemplates()));

    ignorePattern = config.getInitParameter(IGNORE_PATTERN) != null
            ? Pattern.compile(config.getInitParameter(IGNORE_PATTERN))
            : null;/*  w ww .j  a v a  2 s  .  c om*/
    logger.debug("{} = {}", IGNORE_PATTERN, ignorePattern);
}

From source file:com.github.wnameless.spring.papertrail.PaperTrailService.java

@SuppressWarnings({ "unchecked", "deprecation" })
public PaperTrailService(ApplicationContext appCtx) {
    this.appCtx = appCtx;
    EnablePaperTrail ept = getEnablePaperTrailAnno();
    targetMethods = unmodifiableList(Arrays.asList(ept.targetMethods()));
    paperTrailEntityClass = ept.value();
    paperTrailRepo = appCtx.getBean(PaperTrailCrudRepository.class);

    callbacks = appCtx.getBeansOfType(PaperTrailCallback.class);
}

From source file:it.scoppelletti.programmerpower.web.view.AuthorizeComponent.java

/**
 * Restituisce il gestore delle espressioni.
 * //from  w w w. ja v a 2s .  c  o m
 * @return Oggetto.
 */
private DefaultWebSecurityExpressionHandler getExpressionHandler() {
    ServletContext servletCtx;
    ApplicationContext applCtx;
    DefaultWebSecurityExpressionHandler exprHandler;
    Map<String, DefaultWebSecurityExpressionHandler> exprHandlers;

    servletCtx = ServletActionContext.getServletContext();
    applCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletCtx);
    exprHandlers = applCtx.getBeansOfType(DefaultWebSecurityExpressionHandler.class);

    if (exprHandlers.size() == 0) {
        throw new ObjectNotFoundException(DefaultWebSecurityExpressionHandler.class.getName());
    }

    exprHandler = (DefaultWebSecurityExpressionHandler) exprHandlers.values().toArray()[0];

    return exprHandler;
}

From source file:org.jolokia.jvmagent.spring.SpringJolokiaServerTest.java

@Test
public void withMultiConfigAndStart() throws Exception {
    SpringJolokiaAgent server = new SpringJolokiaAgent();
    server.setLookupConfig(true);/*  www .ja  v a 2  s.com*/
    server.setConfig(getConfig(true, 100));

    ApplicationContext ctx = createMock(ApplicationContext.class);
    Map<String, SpringJolokiaConfigHolder> configs = new HashMap<String, SpringJolokiaConfigHolder>();
    configs.put("B", getConfig(false, 10, "executor", "single", "agentContext", "/j4p/"));
    configs.put("A", getConfig(true, 20, "executor", "fixed", "threadNr", "2"));
    expect(ctx.getBeansOfType(SpringJolokiaConfigHolder.class)).andReturn(configs);
    replay(ctx);
    server.setApplicationContext(ctx);
    server.afterPropertiesSet();
    JolokiaServerConfig cfg = server.getServerConfig();
    assertEquals(cfg.getExecutor(), "fixed");
    assertEquals(cfg.getThreadNr(), 2);
    assertEquals(cfg.getContextPath(), "/j4p/");
    checkServerAndStop(server);
}