Java Code Examples for org.springframework.beans.factory.ListableBeanFactory#getBeanNamesForType()

The following examples show how to use org.springframework.beans.factory.ListableBeanFactory#getBeanNamesForType() . 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: SchedulerAccessorBean.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected Scheduler findScheduler(String schedulerName) throws SchedulerException {
	if (this.beanFactory instanceof ListableBeanFactory) {
		ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory;
		String[] beanNames = lbf.getBeanNamesForType(Scheduler.class);
		for (String beanName : beanNames) {
			Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName);
			if (schedulerName.equals(schedulerBean.getSchedulerName())) {
				return schedulerBean;
			}
		}
	}
	Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName);
	if (schedulerInRepo == null) {
		throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found");
	}
	return schedulerInRepo;
}
 
Example 2
Source File: ConfigurationClassProcessingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void configWithFactoryBeanReturnType() {
	ListableBeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class);
	assertEquals(List.class, factory.getType("factoryBean"));
	assertTrue(factory.isTypeMatch("factoryBean", List.class));
	assertEquals(FactoryBean.class, factory.getType("&factoryBean"));
	assertTrue(factory.isTypeMatch("&factoryBean", FactoryBean.class));
	assertFalse(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class));
	assertFalse(factory.isTypeMatch("&factoryBean", ListFactoryBean.class));
	assertTrue(factory.getBean("factoryBean") instanceof List);

	String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(List.class);
	assertEquals("factoryBean", beanNames[0]);
}
 
Example 3
Source File: SchedulerAccessorBean.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected Scheduler findScheduler(String schedulerName) throws SchedulerException {
	if (this.beanFactory instanceof ListableBeanFactory) {
		ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory;
		String[] beanNames = lbf.getBeanNamesForType(Scheduler.class);
		for (String beanName : beanNames) {
			Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName);
			if (schedulerName.equals(schedulerBean.getSchedulerName())) {
				return schedulerBean;
			}
		}
	}
	Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName);
	if (schedulerInRepo == null) {
		throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found");
	}
	return schedulerInRepo;
}
 
Example 4
Source File: SchedulerAccessorBean.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected Scheduler findScheduler(String schedulerName) throws SchedulerException {
	if (this.beanFactory instanceof ListableBeanFactory) {
		ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory;
		String[] beanNames = lbf.getBeanNamesForType(Scheduler.class);
		for (String beanName : beanNames) {
			Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName);
			if (schedulerName.equals(schedulerBean.getSchedulerName())) {
				return schedulerBean;
			}
		}
	}
	Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName);
	if (schedulerInRepo == null) {
		throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found");
	}
	return schedulerInRepo;
}
 
Example 5
Source File: ConfigurationClassProcessingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void configWithFactoryBeanReturnType() {
	ListableBeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class);
	assertEquals(List.class, factory.getType("factoryBean"));
	assertTrue(factory.isTypeMatch("factoryBean", List.class));
	assertEquals(FactoryBean.class, factory.getType("&factoryBean"));
	assertTrue(factory.isTypeMatch("&factoryBean", FactoryBean.class));
	assertFalse(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class));
	assertFalse(factory.isTypeMatch("&factoryBean", ListFactoryBean.class));
	assertTrue(factory.getBean("factoryBean") instanceof List);

	String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(List.class);
	assertEquals("factoryBean", beanNames[0]);
}
 
Example 6
Source File: SchedulerAccessorBean.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected Scheduler findScheduler(String schedulerName) throws SchedulerException {
	if (this.beanFactory instanceof ListableBeanFactory) {
		ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory;
		String[] beanNames = lbf.getBeanNamesForType(Scheduler.class);
		for (String beanName : beanNames) {
			Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName);
			if (schedulerName.equals(schedulerBean.getSchedulerName())) {
				return schedulerBean;
			}
		}
	}
	Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName);
	if (schedulerInRepo == null) {
		throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found");
	}
	return schedulerInRepo;
}
 
Example 7
Source File: JobsManagerImpl.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
private Map<String, JobFactory> loadFactories(ListableBeanFactory beanFactory) {
    ImmutableMap.Builder<String, JobFactory> mb = ImmutableMap.builder();
    //load factory beans
    String[] factoryNames = beanFactory.getBeanNamesForType(JobFactory.class);
    for(String factoryName: factoryNames) {
        JobFactory factory = beanFactory.getBean(factoryName, JobFactory.class);
        if(factory == this) {
            // we do not want to load self
            continue;
        }
        Set<String> types = factory.getTypes();
        for(String type: types) {
            mb.put(type, factory);
        }
    }
    //load job beans
    String[] jobNames = beanFactory.getBeanNamesForAnnotation(JobBean.class);
    for(String jobName: jobNames) {
        Class<?> jobType = beanFactory.getType(jobName);
        JobDescription jd = descFactory.getFor(jobName);
        mb.put(jobName, new JobFactoryForBean(this, jobName, jobType, jd));
    }
    return mb.build();
}
 
Example 8
Source File: ConfigurationClassProcessingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void configWithFactoryBeanReturnType() {
	ListableBeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class);
	assertEquals(List.class, factory.getType("factoryBean"));
	assertTrue(factory.isTypeMatch("factoryBean", List.class));
	assertEquals(FactoryBean.class, factory.getType("&factoryBean"));
	assertTrue(factory.isTypeMatch("&factoryBean", FactoryBean.class));
	assertFalse(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class));
	assertFalse(factory.isTypeMatch("&factoryBean", ListFactoryBean.class));
	assertTrue(factory.getBean("factoryBean") instanceof List);

	String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = factory.getBeanNamesForType(List.class);
	assertEquals("factoryBean", beanNames[0]);
}
 
Example 9
Source File: NotifyManager.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Notifica un evento ai corrispondenti servizi osservatori.
 * @param event L'evento da notificare.
 */
protected void notify(ApsEvent event) {
	ListableBeanFactory factory = (ListableBeanFactory) this._beanFactory;
	String[] defNames = factory.getBeanNamesForType(event.getObserverInterface());
	for (int i=0; i<defNames.length; i++) {
		Object observer = null;
		try {
			observer = this._beanFactory.getBean(defNames[i]);
		} catch (Throwable t) {
			observer = null;
		}
		if (observer != null) {
			((ObserverService) observer).update(event);
			_logger.debug("The event {} was notified to the {} service", event.getClass().getName(), observer.getClass().getName());
		}
	}
	_logger.debug("The {} has been notified", event.getClass().getName());
}
 
Example 10
Source File: BootstrapApplicationListener.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
private <T> List<T> getOrderedBeansOfType(ListableBeanFactory context,
		Class<T> type) {
	List<T> result = new ArrayList<T>();
	for (String name : context.getBeanNamesForType(type)) {
		result.add(context.getBean(name, type));
	}
	AnnotationAwareOrderComparator.sort(result);
	return result;
}
 
Example 11
Source File: AbstractBaseEntityAttributeConfigAction.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected List<String> getEnumeratorExtractorBeans(Class type) {
	List<String> extractors = null;
	try {
		ListableBeanFactory factory = (ListableBeanFactory) this.getBeanFactory();
		String[] defNames = factory.getBeanNamesForType(type);
		extractors = Arrays.asList(defNames);
	} catch (Throwable t) {
		_logger.error("Error while extracting enumerator extractor beans", t);
		throw new RuntimeException("Error while extracting enumerator extractor beans", t);
	}
	return extractors;
}
 
Example 12
Source File: EntityManagersAction.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List<String> getEntityManagers() {
	List<String> serviceNames = null;
	try {
		ListableBeanFactory factory = (ListableBeanFactory) this.getBeanFactory();
		String[] defNames = factory.getBeanNamesForType(IEntityManager.class);
		serviceNames = Arrays.asList(defNames);
	} catch (Throwable t) {
		_logger.error("Error while extracting entity managers", t);
		//ApsSystemUtils.logThrowable(t, this, "getEntityManagers");
		throw new RuntimeException("Error while extracting entity managers", t);
	}
	return serviceNames;
}
 
Example 13
Source File: TestEntityManager.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void init() {
	ListableBeanFactory factory = (ListableBeanFactory) this.getApplicationContext();
	String[] defNames = factory.getBeanNamesForType(IEntityManager.class);
	if (null != defNames && defNames.length>0) {
		this._entityManager = (IEntityManager) this.getApplicationContext().getBean(defNames[0]);
	}
}
 
Example 14
Source File: GuiFragmentDtoBuilder.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected GuiFragmentDto toDto(GuiFragment src) {
	if (null == src) {
		return null;
	}
	WidgetType type = null;
	if (StringUtils.isNotEmpty(src.getWidgetTypeCode())) {
		type = this.getWidgetTypeManager().getWidgetType(src.getWidgetTypeCode());
	}
	GuiFragmentDto dest = new GuiFragmentDto(src, type, langManager);
	ListableBeanFactory factory = (ListableBeanFactory) this.beanFactory;
	String[] defNames = factory.getBeanNamesForType(GuiFragmentUtilizer.class);
	for (String defName : defNames) {
		GuiFragmentUtilizer utilizers = null;
		try {
			utilizers = this.beanFactory.getBean(defName, GuiFragmentUtilizer.class);
			List<Object> references = utilizers.getGuiFragmentUtilizers(src.getCode());
			if (null != references) {
				for (Object reference : references) {
					if (reference instanceof GuiFragment) {
						dest.addFragmentRef((GuiFragment) reference);
					} else if (reference instanceof PageModel) {
						dest.addPageModelRef((PageModel) reference);
					} else {
						logger.info("unexpected reference - type {}", reference.getClass());
					}
				}
			}
		} catch (Throwable t) {
			logger.error("Error extracting reference from bean '{}'", defName);
			utilizers = null;
		}
	}
	return dest;
}
 
Example 15
Source File: AbstractDatabaseUtils.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String[] extractBeanNames(Class beanClass) {
	ListableBeanFactory factory = (ListableBeanFactory) this.getBeanFactory();
	return factory.getBeanNamesForType(beanClass);
}
 
Example 16
Source File: DatabaseManager.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String[] extractBeanNames(Class beanClass) {
    ListableBeanFactory factory = (ListableBeanFactory) this.getBeanFactory();
    return factory.getBeanNamesForType(beanClass);
}
 
Example 17
Source File: VaultReactiveBootstrapConfiguration.java    From spring-cloud-vault with Apache License 2.0 4 votes vote down vote up
/**
 * @param beanFactory the {@link BeanFactory}.
 * @return the {@link VaultTokenSupplier} for reactive Vault session management
 * adapting {@link ClientAuthentication} that also implement
 * {@link AuthenticationStepsFactory}.
 * @see AuthenticationStepsFactory
 */
@Bean
@ConditionalOnMissingBean(name = "vaultTokenSupplier")
@ConditionalOnAuthentication
public VaultTokenSupplier vaultTokenSupplier(ListableBeanFactory beanFactory) {

	Assert.notNull(beanFactory, "BeanFactory must not be null");

	String[] authStepsFactories = beanFactory
			.getBeanNamesForType(AuthenticationStepsFactory.class);

	if (!ObjectUtils.isEmpty(authStepsFactories)) {

		AuthenticationStepsFactory factory = beanFactory
				.getBean(AuthenticationStepsFactory.class);
		return createAuthenticationStepsOperator(factory);
	}

	String[] clientAuthentications = beanFactory
			.getBeanNamesForType(ClientAuthentication.class);

	if (!ObjectUtils.isEmpty(clientAuthentications)) {

		ClientAuthentication clientAuthentication = beanFactory
				.getBean(ClientAuthentication.class);

		if (clientAuthentication instanceof TokenAuthentication) {

			TokenAuthentication authentication = (TokenAuthentication) clientAuthentication;
			return () -> Mono.just(authentication.login());
		}

		if (clientAuthentication instanceof AuthenticationStepsFactory) {
			return createAuthenticationStepsOperator(
					(AuthenticationStepsFactory) clientAuthentication);
		}

		throw new IllegalStateException(String.format(
				"Cannot construct VaultTokenSupplier from %s. "
						+ "ClientAuthentication must implement AuthenticationStepsFactory or be TokenAuthentication",
				clientAuthentication));
	}

	throw new IllegalStateException(
			"Cannot construct VaultTokenSupplier. Please configure VaultTokenSupplier bean named vaultTokenSupplier.");
}