List of usage examples for org.springframework.beans.factory BeanFactory getBean
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
From source file:org.springframework.integration.support.utils.IntegrationUtils.java
private static <T> T getBeanOfType(BeanFactory beanFactory, String beanName, Class<T> type) { Assert.notNull(beanFactory, "BeanFactory must not be null"); if (!beanFactory.containsBean(beanName)) { return null; }// ww w .j a v a 2 s. com return beanFactory.getBean(beanName, type); }
From source file:org.springframework.richclient.application.ApplicationLauncher.java
/** * Searches the given bean factory for a {@link SplashScreen} defined with * the bean name {@link #SPLASH_SCREEN_BEAN_ID} and displays it, if found. * * @param beanFactory The bean factory that is expected to contain the * splash screen bean definition. Must not be null. * * @throws NullPointerException if {@code beanFactory} is null. * @throws BeanNotOfRequiredTypeException if the bean found under the splash * screen bean name is not a {@link SplashScreen}. * *///from w w w .jav a2 s. co m private void displaySplashScreen(BeanFactory beanFactory) { if (beanFactory.containsBean(SPLASH_SCREEN_BEAN_ID)) { this.splashScreen = (SplashScreen) beanFactory.getBean(SPLASH_SCREEN_BEAN_ID, SplashScreen.class); logger.debug("Displaying application splash screen..."); try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { ApplicationLauncher.this.splashScreen.splash(); } }); } catch (Exception e) { throw new RuntimeException("EDT threading issue while showing splash screen", e); } } else { logger.info("No splash screen bean found to display. Continuing..."); } }
From source file:org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.java
private <T> T resolveSchedulerBean(BeanFactory beanFactory, Class<T> schedulerType, boolean byName) { if (byName) { T scheduler = beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType); if (this.beanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName); }//from ww w. j a v a2 s. com return scheduler; } else if (beanFactory instanceof AutowireCapableBeanFactory) { NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) beanFactory).resolveNamedBean(schedulerType); if (this.beanName != null && beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) beanFactory).registerDependentBean(holder.getBeanName(), this.beanName); } return holder.getBeanInstance(); } else { return beanFactory.getBean(schedulerType); } }
From source file:org.springframework.test.context.transaction.TestContextTransactionUtils.java
/** * Retrieve the {@link DataSource} to use for the supplied {@linkplain TestContext * test context}.//from ww w. ja va 2 s . c om * <p>The following algorithm is used to retrieve the {@code DataSource} from * the {@link org.springframework.context.ApplicationContext ApplicationContext} * of the supplied test context: * <ol> * <li>Look up the {@code DataSource} by type and name, if the supplied * {@code name} is non-empty, throwing a {@link BeansException} if the named * {@code DataSource} does not exist. * <li>Attempt to look up the single {@code DataSource} by type. * <li>Attempt to look up the <em>primary</em> {@code DataSource} by type. * <li>Attempt to look up the {@code DataSource} by type and the * {@linkplain #DEFAULT_DATA_SOURCE_NAME default data source name}. * @param testContext the test context for which the {@code DataSource} * should be retrieved; never {@code null} * @param name the name of the {@code DataSource} to retrieve; may be {@code null} * or <em>empty</em> * @return the {@code DataSource} to use, or {@code null} if not found * @throws BeansException if an error occurs while retrieving an explicitly * named {@code DataSource} */ @Nullable public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name) { Assert.notNull(testContext, "TestContext must not be null"); BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory(); try { // look up by type and explicit name if (StringUtils.hasText(name)) { return bf.getBean(name, DataSource.class); } } catch (BeansException ex) { logger.error(String.format("Failed to retrieve DataSource named '%s' for test context %s", name, testContext), ex); throw ex; } try { if (bf instanceof ListableBeanFactory) { ListableBeanFactory lbf = (ListableBeanFactory) bf; // look up single bean by type Map<String, DataSource> dataSources = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DataSource.class); if (dataSources.size() == 1) { return dataSources.values().iterator().next(); } try { // look up single bean by type, with support for 'primary' beans return bf.getBean(DataSource.class); } catch (BeansException ex) { logBeansException(testContext, ex, PlatformTransactionManager.class); } } // look up by type and default name return bf.getBean(DEFAULT_DATA_SOURCE_NAME, DataSource.class); } catch (BeansException ex) { logBeansException(testContext, ex, DataSource.class); return null; } }
From source file:org.springframework.test.context.transaction.TestContextTransactionUtils.java
/** * Retrieve the {@linkplain PlatformTransactionManager transaction manager} * to use for the supplied {@linkplain TestContext test context}. * <p>The following algorithm is used to retrieve the transaction manager * from the {@link org.springframework.context.ApplicationContext ApplicationContext} * of the supplied test context://from w w w. ja v a2 s .c om * <ol> * <li>Look up the transaction manager by type and explicit name, if the supplied * {@code name} is non-empty, throwing a {@link BeansException} if the named * transaction manager does not exist. * <li>Attempt to look up the single transaction manager by type. * <li>Attempt to look up the <em>primary</em> transaction manager by type. * <li>Attempt to look up the transaction manager via a * {@link TransactionManagementConfigurer}, if present. * <li>Attempt to look up the transaction manager by type and the * {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager * name}. * @param testContext the test context for which the transaction manager * should be retrieved; never {@code null} * @param name the name of the transaction manager to retrieve; may be * {@code null} or <em>empty</em> * @return the transaction manager to use, or {@code null} if not found * @throws BeansException if an error occurs while retrieving an explicitly * named transaction manager * @throws IllegalStateException if more than one TransactionManagementConfigurer * exists in the ApplicationContext */ @Nullable public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable String name) { Assert.notNull(testContext, "TestContext must not be null"); BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory(); try { // look up by type and explicit name if (StringUtils.hasText(name)) { return bf.getBean(name, PlatformTransactionManager.class); } } catch (BeansException ex) { logger.error(String.format("Failed to retrieve transaction manager named '%s' for test context %s", name, testContext), ex); throw ex; } try { if (bf instanceof ListableBeanFactory) { ListableBeanFactory lbf = (ListableBeanFactory) bf; // look up single bean by type Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, PlatformTransactionManager.class); if (txMgrs.size() == 1) { return txMgrs.values().iterator().next(); } try { // look up single bean by type, with support for 'primary' beans return bf.getBean(PlatformTransactionManager.class); } catch (BeansException ex) { logBeansException(testContext, ex, PlatformTransactionManager.class); } // look up single TransactionManagementConfigurer Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils .beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class); Assert.state(configurers.size() <= 1, "Only one TransactionManagementConfigurer may exist in the ApplicationContext"); if (configurers.size() == 1) { return configurers.values().iterator().next().annotationDrivenTransactionManager(); } } // look up by type and default name return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class); } catch (BeansException ex) { logBeansException(testContext, ex, PlatformTransactionManager.class); return null; } }