Java Code Examples for org.springframework.context.ApplicationContext#getBeansOfType()

The following examples show how to use org.springframework.context.ApplicationContext#getBeansOfType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteSpringHelperImpl.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public <T> IgniteBiTuple<Collection<T>, ? extends GridSpringResourceContext> loadConfigurations(
    InputStream cfgStream, Class<T> cls, String... excludedProps) throws IgniteCheckedException {
    ApplicationContext springCtx = applicationContext(cfgStream, excludedProps);
    Map<String, T> cfgMap;

    try {
        cfgMap = springCtx.getBeansOfType(cls);
    }
    catch (BeansException e) {
        throw new IgniteCheckedException("Failed to instantiate bean [type=" + cls +
            ", err=" + e.getMessage() + ']', e);
    }

    if (cfgMap == null || cfgMap.isEmpty())
        throw new IgniteCheckedException("Failed to find configuration in: " + cfgStream);

    return F.t(cfgMap.values(), new GridSpringResourceContextImpl(springCtx));
}
 
Example 2
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void emptyHandlerMappings() {
	ApplicationContext context = initContext(WebConfig.class);

	Map<String, HandlerMapping> handlerMappings = context.getBeansOfType(HandlerMapping.class);
	assertFalse(handlerMappings.containsKey("viewControllerHandlerMapping"));
	assertFalse(handlerMappings.containsKey("resourceHandlerMapping"));
	assertFalse(handlerMappings.containsKey("defaultServletHandlerMapping"));

	Object nullBean = context.getBean("viewControllerHandlerMapping");
	assertTrue(nullBean.equals(null));

	nullBean = context.getBean("resourceHandlerMapping");
	assertTrue(nullBean.equals(null));

	nullBean = context.getBean("defaultServletHandlerMapping");
	assertTrue(nullBean.equals(null));
}
 
Example 3
Source File: DistributedConfigurationManagerTest.java    From oodt with Apache License 2.0 6 votes vote down vote up
@Before
public void setUpTest() throws Exception {
    System.setProperty("org.apache.oodt.cas.cli.action.spring.config", "src/main/resources/cmd-line-actions.xml");
    System.setProperty("org.apache.oodt.cas.cli.option.spring.config", "src/main/resources/cmd-line-options.xml");

    ConfigPublisher.main(new String[]{
            "-connectString", zookeeper.getConnectString(),
            "-config", CONFIG_PUBLISHER_XML,
            "-a", "publish"
    });

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG_PUBLISHER_XML);
    Map distributedConfigurationPublishers = applicationContext.getBeansOfType(DistributedConfigurationPublisher.class);

    publishers = new ArrayList<>();
    configurationManagers = new HashMap<>();
    for (Object bean : distributedConfigurationPublishers.values()) {
        DistributedConfigurationPublisher publisher = (DistributedConfigurationPublisher) bean;

        System.setProperty(OODT_PROJECT, publisher.getProject());
        System.setProperty(publisher.getComponent().getHome(), ".");
        publishers.add(publisher);
        configurationManagers.put(publisher.getComponent(), new DistributedConfigurationManager(publisher.getComponent()));
        System.clearProperty(OODT_PROJECT);
    }
}
 
Example 4
Source File: ServerEndpointExporter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Actually register the endpoints. Called by {@link #afterSingletonsInstantiated()}.
 */
protected void registerEndpoints() {
	Set<Class<?>> endpointClasses = new LinkedHashSet<>();
	if (this.annotatedEndpointClasses != null) {
		endpointClasses.addAll(this.annotatedEndpointClasses);
	}

	ApplicationContext context = getApplicationContext();
	if (context != null) {
		String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);
		for (String beanName : endpointBeanNames) {
			endpointClasses.add(context.getType(beanName));
		}
	}

	for (Class<?> endpointClass : endpointClasses) {
		registerEndpoint(endpointClass);
	}

	if (context != null) {
		Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class);
		for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) {
			registerEndpoint(endpointConfig);
		}
	}
}
 
Example 5
Source File: AopUtils.java    From springboot-plugin-framework-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 解决AOP无法代理到插件类的问题
 * @param applicationContext 插件包装类
 */
public static synchronized void registered(ApplicationContext applicationContext) {
    Map<String, ProxyProcessorSupport> beansOfType = applicationContext
            .getBeansOfType(ProxyProcessorSupport.class);
    if(beansOfType.isEmpty()){
        LOG.warn("Not found ProxyProcessorSupports, And Plugin AOP can't used");
        return;
    }
    for (ProxyProcessorSupport support : beansOfType.values()) {
        if(support == null){
            continue;
        }
        ProxyWrapper proxyWrapper = new ProxyWrapper();
        proxyWrapper.setProxyProcessorSupport(support);
        PROXY_WRAPPERS.add(proxyWrapper);
    }
}
 
Example 6
Source File: GaugeRegistry4SpringContext.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
    //仅在root application context启动后执行
    if (applicationContext.getParent() != null) {
        return;
    }
    Map<String, Object> beansOfType = applicationContext.getBeansOfType(Object.class);
    for (Object bean : beansOfType.values()) {
        if (bean == null) {
            return;
        }
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        Method[] methods = targetClass.getMethods();
        if (methods == null || methods.length == 0) {
            return;
        }
        for (Method method : methods) {
            EnableGauge aliGaugeAnnotation = method.getAnnotation(EnableGauge.class);
            if (aliGaugeAnnotation == null) {
                continue;
            }
            GaugeRegistry.registerGauge(aliGaugeAnnotation, bean, method);
        }
    }
}
 
Example 7
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 6 votes vote down vote up
private void bindWithRequestAndPropertyValues(ServletRequest request, MutablePropertyValues mpvs) {
	GrailsWebRequest webRequest = GrailsWebRequest.lookup((HttpServletRequest) request);
	if (webRequest != null) {
		final ApplicationContext applicationContext = webRequest.getApplicationContext();
		if (applicationContext != null) {
			final Map<String, BindEventListener> bindEventListenerMap = applicationContext.getBeansOfType(BindEventListener.class);
			for (BindEventListener bindEventListener : bindEventListenerMap.values()) {
				bindEventListener.doBind(getTarget(), mpvs, getTypeConverter());
			}
		}
	}
	preProcessMutablePropertyValues(mpvs);

	if (request instanceof MultipartHttpServletRequest) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
	}
	doBind(mpvs);
}
 
Example 8
Source File: SpringBootServiceLocator.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> Collection<Class<T>> locate(Class<T> clazz) {
	Set<Class<T>> result = new LinkedHashSet<Class<T>>();

	// use the Spring API to obtain the WebApplicationContext
	ApplicationContext context = ApplicationContextProvider.getApplicationContext();

	// may be null if Spring hasn't started yet
	if (context != null) {

		// ask spring about SPI implementations
		Map<String, T> beans = context.getBeansOfType(clazz);

		// add the implementations Class objects to the result set
		for (T type : beans.values()) {
			result.add((Class<T>) type.getClass());
		}

	}

	return result;
}
 
Example 9
Source File: ContextNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void propertyPlaceholderSystemProperties() throws Exception {
	String value = System.setProperty("foo", "spam");
	try {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"contextNamespaceHandlerTests-system.xml", getClass());
		Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
				.getBeansOfType(PropertyPlaceholderConfigurer.class);
		assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
		assertEquals("spam", applicationContext.getBean("string"));
		assertEquals("none", applicationContext.getBean("fallback"));
	}
	finally {
		if (value != null) {
			System.setProperty("foo", value);
		}
	}
}
 
Example 10
Source File: RpcProviderProcessor.java    From hasting with MIT License 5 votes vote down vote up
private void initRpcServer(ApplicationContext apc){
	Map<String, AbstractRpcServer> servers = apc.getBeansOfType(AbstractRpcServer.class);
	Set<String> keys = servers.keySet();
	for(String key:keys){
		if(!StringUtils.hasText(key)){
			key = DEFAULT_RPC_BEAN;
		}
		AbstractRpcServer server = servers.get(key);
		serverMap.put(key, server);
		logger.info("find rpc service:"+key+" host:"+server.getHost()+":"+server.getPort());
	}
}
 
Example 11
Source File: SpringDataExtensionInitializerLocator.java    From yanwte2 with Apache License 2.0 5 votes vote down vote up
@Override
public Set<DataExtensionInitializer<ExtensibleData, ?>> getInitializers() {
    ApplicationContext applicationContext = SpringHook.getApplicationContext();
    @SuppressWarnings("unchecked")
    Map<String, DataExtensionInitializer<ExtensibleData, ?>> beansOfType =
            (Map) applicationContext.getBeansOfType(DataExtensionInitializer.class);
    if (beansOfType != null) {
        Collection<DataExtensionInitializer<ExtensibleData, ?>> initializers =
                beansOfType.values();
        return ImmutableSet.copyOf(initializers);
    }
    return ImmutableSet.of();
}
 
Example 12
Source File: JobFactoryRegistrar.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void register(ContextRefreshedEvent event) {
  ApplicationContext ctx = event.getApplicationContext();
  Map<String, JobFactory> jobFactoryMap = ctx.getBeansOfType(JobFactory.class);
  jobFactoryMap.values().forEach(this::register);
}
 
Example 13
Source File: ExceptionResponseGeneratorRegistrar.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void register(ApplicationContext applicationContext) {
  Map<String, ExceptionResponseGenerator> responseGeneratorMap =
      applicationContext.getBeansOfType(ExceptionResponseGenerator.class);
  responseGeneratorMap.values().forEach(this::registerResponseGenerator);
}
 
Example 14
Source File: ScriptRunnerRegistrar.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void register(ContextRefreshedEvent event) {
  ApplicationContext ctx = event.getApplicationContext();
  Map<String, ScriptRunner> scriptRunnerMap = ctx.getBeansOfType(ScriptRunner.class);
  scriptRunnerMap.values().forEach(this::register);
}
 
Example 15
Source File: EmbeddedVelocityViewResolverApplicationListener.java    From velocity-spring-boot-project with Apache License 2.0 3 votes vote down vote up
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    ApplicationContext applicationContext = event.getApplicationContext();

    Map<String, EmbeddedVelocityViewResolver> viewResolverMap =
            applicationContext.getBeansOfType(EmbeddedVelocityViewResolver.class);

    for (EmbeddedVelocityViewResolver viewResolver : viewResolverMap.values()) {

        viewResolver.setViewClass(EmbeddedVelocityToolboxView.class);

    }

}
 
Example 16
Source File: DocumentsView.java    From cia with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new document view.
 *
 * @param context
 *            the context
 */
public DocumentsView(final ApplicationContext context) {
	super(context.getBeansOfType(PageModeContentFactory.class), NAME);

}
 
Example 17
Source File: AgentOperationView.java    From cia with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new agent operation view.
 *
 * @param context
 *            the context
 */
public AgentOperationView(final ApplicationContext context) {
	super(context.getBeansOfType(PageModeContentFactory.class), NAME);
}
 
Example 18
Source File: AdminApplicationConfigurationView.java    From cia with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new admin application configuration view.
 *
 * @param context
 *            the context
 */
public AdminApplicationConfigurationView(final ApplicationContext context) {
	super(context.getBeansOfType(PageModeContentFactory.class), NAME);
}
 
Example 19
Source File: MinistryView.java    From cia with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new ministry view.
 *
 * @param context
 *            the context
 */
public MinistryView(final ApplicationContext context) {
	super(context.getBeansOfType(PageModeContentFactory.class), NAME);
}
 
Example 20
Source File: AdminCountryView.java    From cia with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiates a new admin country view.
 *
 * @param context
 *            the context
 */
public AdminCountryView(final ApplicationContext context) {
	super(context.getBeansOfType(PageModeContentFactory.class), NAME);
}