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; }
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); for (String candidateName : candidateNames) { EntityManagerFactory emf = (EntityManagerFactory) beanFactory.getBean(candidateName); if (emf instanceof EntityManagerFactoryInfo && unitName.equals(((EntityManagerFactoryInfo) emf).getPersistenceUnitName())) { return beanFactory.getBean(unitName, EntityManagerFactory.class); return beanFactory.getBean(EntityManagerFactory.class);
WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class); if (wsAnnotation != null || wsProviderAnnotation != null) { Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName)); if (this.endpointProperties != null) { endpoint.setProperties(this.endpointProperties);
Object bean = this.beanFactory.getBean(beanName); ObjectName objectName = registerBeanInstance(bean, beanKey); replaceNotificationListenerBeanNameKeysIfNecessary(beanName, objectName);
private Object invokeServiceLocatorMethod(Method method, Object[] args) throws Exception { Class<?> serviceLocatorMethodReturnType = getServiceLocatorMethodReturnType(method); try { String beanName = tryGetBeanName(args); Assert.state(beanFactory != null, "No BeanFactory available"); if (StringUtils.hasLength(beanName)) { // Service locator for a specific bean name return beanFactory.getBean(beanName, serviceLocatorMethodReturnType); } else { // Service locator for a bean type return beanFactory.getBean(serviceLocatorMethodReturnType); } } catch (BeansException ex) { if (serviceLocatorExceptionConstructor != null) { throw createServiceLocatorException(serviceLocatorExceptionConstructor, ex); } throw ex; } }
private <T> List<Entry<String, T>> getOrderedBeansOfType( ListableBeanFactory beanFactory, Class<T> type, Set<?> excludes) { String[] names = beanFactory.getBeanNamesForType(type, true, false); Map<String, T> map = new LinkedHashMap<>(); for (String name : names) { if (!excludes.contains(name) && !ScopedProxyUtils.isScopedTarget(name)) { T bean = beanFactory.getBean(name, type); if (!excludes.contains(bean)) { map.put(name, bean); } } } List<Entry<String, T>> beans = new ArrayList<>(); beans.addAll(map.entrySet()); beans.sort((o1, o2) -> AnnotationAwareOrderComparator.INSTANCE .compare(o1.getValue(), o2.getValue())); return beans; }
Object beanInstance = null; if (!lazyInit) { beanInstance = this.beanFactory.getBean(beanName); if (!beanClass.isInstance(beanInstance)) { continue;
/** * Gets name-matched Beans from {@link ListableBeanFactory BeanFactory} * * @param beanFactory {@link ListableBeanFactory BeanFactory} * @param beanNames the names of Bean * @param beanType the {@link Class type} of Bean * @param <T> the {@link Class type} of Bean * @return the read-only and non-null {@link List} of Bean names * @since 2.6.6 */ public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); List<T> beans = new ArrayList<T>(beanNames.length); for (String beanName : beanNames) { if (containsElement(allBeanNames, beanName)) { beans.add(beanFactory.getBean(beanName, beanType)); } } return Collections.unmodifiableList(beans); } }
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; }
private void testSingleTestBean(ListableBeanFactory lbf) { assertTrue("1 beans defined", lbf.getBeanDefinitionCount() == 1); String[] names = lbf.getBeanDefinitionNames(); assertTrue(names != lbf.getBeanDefinitionNames()); assertTrue("Array length == 1", names.length == 1); assertTrue("0th element == test", names[0].equals("test")); TestBean tb = (TestBean) lbf.getBean("test"); assertTrue("Test is non null", tb != null); assertTrue("Test bean name is Tony", "Tony".equals(tb.getName())); assertTrue("Test bean age is 48", tb.getAge() == 48); }
/** * Gets name-matched Beans from {@link ListableBeanFactory BeanFactory} * * @param beanFactory {@link ListableBeanFactory BeanFactory} * @param beanNames the names of Bean * @param beanType the {@link Class type} of Bean * @param <T> the {@link Class type} of Bean * @return the read-only and non-null {@link List} of Bean names * @since 2.6.6 */ public static <T> List<T> getBeans(ListableBeanFactory beanFactory, String[] beanNames, Class<T> beanType) { String[] allBeanNames = beanNamesForTypeIncludingAncestors(beanFactory, beanType); List<T> beans = new ArrayList<T>(beanNames.length); for (String beanName : beanNames) { if (containsElement(allBeanNames, beanName)) { beans.add(beanFactory.getBean(beanName, beanType)); } } return Collections.unmodifiableList(beans); } }
@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]); }
/** * Add all global interceptors and pointcuts. */ private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) { String[] globalAdvisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Advisor.class); String[] globalInterceptorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, Interceptor.class); List<Object> beans = new ArrayList<>(globalAdvisorNames.length + globalInterceptorNames.length); Map<Object, String> names = new HashMap<>(beans.size()); for (String name : globalAdvisorNames) { Object bean = beanFactory.getBean(name); beans.add(bean); names.put(bean, name); } for (String name : globalInterceptorNames) { Object bean = beanFactory.getBean(name); beans.add(bean); names.put(bean, name); } AnnotationAwareOrderComparator.sort(beans); for (Object bean : beans) { String name = names.get(bean); if (name.startsWith(prefix)) { addAdvisorOnChainCreation(bean, name); } } }
/** * Retrieve all bean of type {@code T} from the given {@code BeanFactory} declaring a * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given * qualifier, or having a bean name matching the given qualifier. * @param beanFactory the factory to get the target beans from (also searching ancestors) * @param beanType the type of beans to retrieve * @param qualifier the qualifier for selecting among all type matches * @return the matching beans of type {@code T} * @throws BeansException if any of the matching beans could not be created * @since 5.1.1 * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class) */ public static <T> Map<String, T> qualifiedBeansOfType( ListableBeanFactory beanFactory, Class<T> beanType, String qualifier) throws BeansException { String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanType); Map<String, T> result = new LinkedHashMap<>(4); for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, beanFactory)) { result.put(beanName, beanFactory.getBean(beanName, beanType)); } } return result; }
private Object invokeServiceLocatorMethod(Method method, Object[] args) throws Exception { Class<?> serviceLocatorMethodReturnType = getServiceLocatorMethodReturnType(method); try { String beanName = tryGetBeanName(args); Assert.state(beanFactory != null, "No BeanFactory available"); if (StringUtils.hasLength(beanName)) { // Service locator for a specific bean name return beanFactory.getBean(beanName, serviceLocatorMethodReturnType); } else { // Service locator for a bean type return beanFactory.getBean(serviceLocatorMethodReturnType); } } catch (BeansException ex) { if (serviceLocatorExceptionConstructor != null) { throw createServiceLocatorException(serviceLocatorExceptionConstructor, ex); } throw ex; } }
/** * Find a single default EntityManagerFactory in the Spring application context. * @return the default EntityManagerFactory * @throws NoSuchBeanDefinitionException if there is no single EntityManagerFactory in the context */ protected EntityManagerFactory findDefaultEntityManagerFactory(@Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); if (this.beanFactory instanceof ConfigurableListableBeanFactory) { // Fancy variant with dependency registration ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory; NamedBeanHolder<EntityManagerFactory> emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class); if (requestingBeanName != null) { clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName); } return emfHolder.getBeanInstance(); } else { // Plain variant: just find a default bean return this.beanFactory.getBean(EntityManagerFactory.class); } }
/** * Retrieve all bean of type {@code T} from the given {@code BeanFactory} declaring a * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given * qualifier, or having a bean name matching the given qualifier. * @param beanFactory the factory to get the target beans from (also searching ancestors) * @param beanType the type of beans to retrieve * @param qualifier the qualifier for selecting among all type matches * @return the matching beans of type {@code T} * @throws BeansException if any of the matching beans could not be created * @since 5.1.1 * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class) */ public static <T> Map<String, T> qualifiedBeansOfType( ListableBeanFactory beanFactory, Class<T> beanType, String qualifier) throws BeansException { String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanType); Map<String, T> result = new LinkedHashMap<>(4); for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, beanFactory)) { result.put(beanName, beanFactory.getBean(beanName, beanType)); } } return result; }
/** * Find a single default EntityManagerFactory in the Spring application context. * @return the default EntityManagerFactory * @throws NoSuchBeanDefinitionException if there is no single EntityManagerFactory in the context */ protected EntityManagerFactory findDefaultEntityManagerFactory(@Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); if (this.beanFactory instanceof ConfigurableListableBeanFactory) { // Fancy variant with dependency registration ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory; NamedBeanHolder<EntityManagerFactory> emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class); if (requestingBeanName != null) { clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName); } return emfHolder.getBeanInstance(); } else { // Plain variant: just find a default bean return this.beanFactory.getBean(EntityManagerFactory.class); } }