@Test public void testAutowireConstructor() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); ConstructorDependenciesBean bean = (ConstructorDependenciesBean) lbf.autowire(ConstructorDependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true); Object spouse = lbf.getBean("spouse"); assertTrue(bean.getSpouse1() == spouse); assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse); }
@Test public void testAutowireBeanByName() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); TestBean spouse = (TestBean) lbf.getBean("spouse"); assertEquals(spouse, bean.getSpouse()); assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse); }
@Test public void testAutowireExistingBeanByName() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); DependenciesBean existingBean = new DependenciesBean(); lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); TestBean spouse = (TestBean) lbf.getBean("spouse"); assertEquals(existingBean.getSpouse(), spouse); assertSame(spouse, BeanFactoryUtils.beanOfType(lbf, TestBean.class)); }
private SessionFactory getSessionFactory() { if (sessionFactory == null) { sessionFactory = BeanFactoryUtils.beanOfType(applicationContext, SessionFactory.class); } return sessionFactory; }
public static<T> T beanOfType(ListableBeanFactory lbf, Class<T> type) { return (T) BeanFactoryUtils.beanOfType(lbf, type); }
private <T> T retrieveBean(final Class<T> objectClass, final String beanName) { T object = null; if (StringUtils.isNoneBlank(beanName) && applicationContext != null && applicationContext.containsBean(beanName)) { object = (T) applicationContext.getBean(beanName); } else { object = BeanFactoryUtils.beanOfType(applicationContext, objectClass); } return object; }
@Override public void afterPropertiesSet() throws Exception { MessageSourceHolder.messageSource = BeanFactoryUtils.beanOfType(getApplicationContext(), NestedMessageSource.class); }
private void configureFromContext(ApplicationContext context) { try { ReloadableDispatcherServletConfig config = BeanFactoryUtils.beanOfType(context, ReloadableDispatcherServletConfig.class); setPublishContext(true); setReloadable(config.isReloadable()); } catch (NoSuchBeanDefinitionException e) { } }
private Optional<Object> prepareInstaller( AcrossModule module, Class<?> installerClass, Optional<Object> installerInstance ) { AcrossConfigurableApplicationContext installerContext = getInstallerContext( module ); if ( !installerInstance.isPresent() ) { try { return Optional.ofNullable( BeanFactoryUtils.beanOfType( installerContext, installerClass ) ); } catch ( NoSuchBeanDefinitionException nsbe ) { return Optional.empty(); } } else { // For compatibility reasons LOG.warn( "Installer {} for module {} was passed as an instance - this functionality will be removed in future releases.", installerClass.getName(), module.getName() ); Object installer = installerInstance.get(); AutowireCapableBeanFactory beanFactory = installerContext.getAutowireCapableBeanFactory(); beanFactory.autowireBeanProperties( installer, AutowireCapableBeanFactory.AUTOWIRE_NO, false ); beanFactory.initializeBean( installer, "" ); return installerInstance; } }