List of usage examples for org.springframework.context.support GenericApplicationContext getBeanNamesForType
@Override
public String[] getBeanNamesForType(ResolvableType type)
From source file:org.jboss.fuse.wsdl2rest.util.SpringCamelContextFactory.java
private static List<SpringCamelContext> createCamelContextList(Resource resource, ClassLoader classLoader) throws Exception { if (classLoader == null) classLoader = SpringCamelContextFactory.class.getClassLoader(); GenericApplicationContext appContext = new GenericApplicationContext(); appContext.setClassLoader(classLoader); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext) { @Override/*from ww w .ja va2s . com*/ protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() { NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver(); return new CamelNamespaceHandlerResolver(defaultResolver); } }; xmlReader.loadBeanDefinitions(resource); SpringCamelContext.setNoStart(true); appContext.refresh(); List<SpringCamelContext> result = new ArrayList<>(); for (String name : appContext.getBeanNamesForType(SpringCamelContext.class)) { result.add(appContext.getBean(name, SpringCamelContext.class)); } return Collections.unmodifiableList(result); }
From source file:com.opengamma.component.factory.AbstractSpringComponentFactory.java
/** * Registers a set of beans by type.// w w w .jav a2 s . c om * * @param <T> the type * @param repo the repository to register in, not null * @param type the type of bean to extract from the Spring context, not null * @param appContext the Spring context, not null */ protected <T> void registerInfrastructureByType(ComponentRepository repo, Class<T> type, GenericApplicationContext appContext) { String[] beanNames = appContext.getBeanNamesForType(type); for (String beanName : beanNames) { T bean = appContext.getBean(beanName, type); String name = simplifyName(type, beanName); repo.registerComponent(type, name, bean); String[] aliases = appContext.getAliases(beanName); for (String alias : aliases) { name = simplifyName(type, alias); repo.registerComponent(type, name, bean); } } }
From source file:atunit.spring.SpringContainer.java
public Object createTest(Class<?> testClass, Map<Field, Object> fieldValues) throws Exception { GenericApplicationContext ctx = new GenericApplicationContext(); for (Field field : fieldValues.keySet()) { Bean beanAnno = field.getAnnotation(Bean.class); AbstractBeanDefinition beandef = defineInstanceHolderFactoryBean(field.getType(), fieldValues.get(field)); if ((beanAnno != null) && !beanAnno.value().equals("")) { ctx.registerBeanDefinition(beanAnno.value(), beandef); } else {// w w w. j ava 2 s . c om BeanDefinitionReaderUtils.registerWithGeneratedName(beandef, ctx); } } loadBeanDefinitions(testClass, ctx); fillInMissingFieldBeans(testClass, ctx); ctx.refresh(); Object test = testClass.newInstance(); for (Field field : testClass.getDeclaredFields()) { field.setAccessible(true); Bean beanAnno = field.getAnnotation(Bean.class); if (beanAnno == null) { if (fieldValues.containsKey(field)) { field.set(test, fieldValues.get(field)); } } else { if (!beanAnno.value().equals("")) { field.set(test, ctx.getBean(beanAnno.value())); } else { String[] beanNames = ctx.getBeanNamesForType(field.getType()); if (beanNames.length < 1) { throw new BeanCreationException("There are no beans defined with type " + field.getType()); } if (beanNames.length > 1) { throw new BeanCreationException( "There are " + beanNames.length + " beans defined with type " + field.getType() + "; consider wiring by name instead"); } field.set(test, ctx.getBean(beanNames[0])); } } } return test; }