org.springframework.beans.factory.config.AutowireCapableBeanFactory Java Examples

The following examples show how to use org.springframework.beans.factory.config.AutowireCapableBeanFactory. 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: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoPrimaryCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertNotNull("Exception should have cause", ex.getCause());
		assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass());
	}
}
 
Example #2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowireWithTwoMatchesForConstructorDependency() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("rod", bd);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("rod2", bd2);
	try {
		lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertTrue(ex.getMessage().contains("rod"));
		assertTrue(ex.getMessage().contains("rod2"));
	}
}
 
Example #3
Source File: WebAbstractDataGrid.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Renderer instance) {
    AutowireCapableBeanFactory autowireBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireBeanFactory.autowireBean(instance);

    if (instance instanceof ApplicationContextAware) {
        ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
    }

    if (instance instanceof InitializingBean) {
        try {
            ((InitializingBean) instance).afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException("Unable to initialize Renderer - calling afterPropertiesSet for " +
                    instance.getClass(), e);
        }
    }
}
 
Example #4
Source File: WebUiComponents.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Component instance) {
    AutowireCapableBeanFactory autowireBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireBeanFactory.autowireBean(instance);

    if (instance instanceof ApplicationContextAware) {
        ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
    }

    if (instance instanceof InitializingBean) {
        try {
            ((InitializingBean) instance).afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to initialize UI Component - calling afterPropertiesSet for " +
                            instance.getClass(), e);
        }
    }
}
 
Example #5
Source File: DesktopComponentsFactory.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Component instance) {
    AutowireCapableBeanFactory autowireBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireBeanFactory.autowireBean(instance);

    if (instance instanceof ApplicationContextAware) {
        ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
    }

    if (instance instanceof InitializingBean) {
        try {
            ((InitializingBean) instance).afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to initialize Component - calling afterPropertiesSet for " +
                            instance.getClass());
        }
    }
}
 
Example #6
Source File: MtasUimaParser.java    From inception with Apache License 2.0 6 votes vote down vote up
public MtasUimaParser(MtasConfiguration config)
{
    super(config);
    
    // Perform dependency injection
    AutowireCapableBeanFactory factory = ApplicationContextProvider.getApplicationContext()
            .getAutowireCapableBeanFactory();
    factory.autowireBean(this);
    factory.initializeBean(this, "transientParser");
    
    JSONObject jsonParserConfiguration = new JSONObject(
            config.attributes.get(ARGUMENT_PARSER_ARGS));
    MtasDocumentIndex index = MtasDocumentIndex
            .getIndex(jsonParserConfiguration.getLong(PARAM_PROJECT_ID));

    // Initialize and populate the hash maps for the layers and features
    for (AnnotationFeature feature : index.getFeaturesToIndex()) {
        layers.put(feature.getLayer().getName(), feature.getLayer());
        layerFeatures
                .computeIfAbsent(feature.getLayer().getName(), key -> new ArrayList<>())
                .add(feature);
    }        
}
 
Example #7
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoPrimaryCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertNotNull("Exception should have cause", ex.getCause());
		assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass());
	}
}
 
Example #8
Source File: OntologyRepositoryCollectionTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@BeforeEach
void beforeMethod() throws IOException, OWLOntologyCreationException, NoSuchMethodException {

  // ontology repository collection is not spring managed, see FileRepositoryCollectionFactory
  File file = ResourceUtils.getFile("small_test_data_NGtest.owl.zip");
  OntologyRepositoryCollection ontologyRepoCollection =
      BeanUtils.instantiateClass(
          OntologyRepositoryCollection.class.getConstructor(File.class), file);
  autowireCapableBeanFactory.autowireBeanProperties(
      ontologyRepoCollection, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
  ontologyRepoCollection.init();

  ontologyRepository = ontologyRepoCollection.getRepository(ONTOLOGY);
  ontologyTermDynamicAnnotationRepository =
      ontologyRepoCollection.getRepository(ONTOLOGY_TERM_DYNAMIC_ANNOTATION);
  ontologyTermNodePathRepository = ontologyRepoCollection.getRepository(ONTOLOGY_TERM_NODE_PATH);
  ontologyTermSynonymRepository = ontologyRepoCollection.getRepository(ONTOLOGY_TERM_SYNONYM);
  ontologyTermRepository = ontologyRepoCollection.getRepository(ONTOLOGY_TERM);
}
 
Example #9
Source File: ScheduledAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private <T> T resolveSchedulerBean(Class<T> schedulerType, boolean byName) {
	if (byName) {
		T scheduler = this.beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType);
		if (this.beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
					DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName);
		}
		return scheduler;
	}
	else if (this.beanFactory instanceof AutowireCapableBeanFactory) {
		NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) this.beanFactory).resolveNamedBean(schedulerType);
		if (this.beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
					holder.getBeanName(), this.beanName);
		}
		return holder.getBeanInstance();
	}
	else {
		return this.beanFactory.getBean(schedulerType);
	}
}
 
Example #10
Source File: SpringBeanContainer.java    From java-technology-stack with MIT License 6 votes vote down vote up
private SpringContainedBean<?> createBean(
		String name, Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) {

	try {
		if (lifecycleOptions.useJpaCompliantCreation()) {
			Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
			this.beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
			this.beanFactory.applyBeanPropertyValues(bean, name);
			bean = this.beanFactory.initializeBean(bean, name);
			return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance));
		}
		else {
			return new SpringContainedBean<>(this.beanFactory.getBean(name, beanType));
		}
	}
	catch (BeansException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug("Falling back to Hibernate's default producer after bean creation failure for " +
					beanType + ": " + ex);
		}
		return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(name, beanType));
	}
}
 
Example #11
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowireWithSatisfiedJavaBeanDependency() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("name", "Rod");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("rod", bd);
	assertEquals(1, lbf.getBeanDefinitionCount());
	// Depends on age, name and spouse (TestBean)
	Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertEquals(1, lbf.getBeanDefinitionCount());
	DependenciesBean kerry = (DependenciesBean) registered;
	TestBean rod = (TestBean) lbf.getBean("rod");
	assertSame(rod, kerry.getSpouse());
}
 
Example #12
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoMatches() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);
	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertTrue(ex.getMessage().contains("test"));
		assertTrue(ex.getMessage().contains("spouse"));
	}
}
 
Example #13
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithIdenticalPriorityCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
	RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertNotNull("Exception should have cause", ex.getCause());
		assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass());
		assertTrue(ex.getMessage().contains("5"));  // conflicting priority
	}
}
 
Example #14
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoMatches() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);
	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertTrue(ex.getMessage().contains("test"));
		assertTrue(ex.getMessage().contains("spouse"));
	}
}
 
Example #15
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanWithFactoryBeanByTypeWithPrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(LazyInitFactory.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(LazyInitFactory.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	LazyInitFactory bd1FactoryBean = (LazyInitFactory) lbf.getBean("&bd1");
	LazyInitFactory bd2FactoryBean = (LazyInitFactory) lbf.getBean("&bd2");
	assertNotNull(bd1FactoryBean);
	assertNotNull(bd2FactoryBean);
	FactoryBeanDependentBean bean = (FactoryBeanDependentBean) lbf.autowire(FactoryBeanDependentBean.class,
			AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertNotEquals(bd1FactoryBean, bean.getFactoryBean());
	assertEquals(bd2FactoryBean, bean.getFactoryBean());
}
 
Example #16
Source File: ActionsImpl.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void autowireContext(Action instance) {
    AutowireCapableBeanFactory autowireBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireBeanFactory.autowireBean(instance);

    if (instance instanceof ApplicationContextAware) {
        ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
    }

    if (instance instanceof InitializingBean) {
        try {
            ((InitializingBean) instance).afterPropertiesSet();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to initialize UI Component - calling afterPropertiesSet for " +
                            instance.getClass(), e);
        }
    }
}
 
Example #17
Source File: EndpointDefinitionParser.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static final void setBlocking(ApplicationContext ctx, EndpointImpl impl) {
    AutowireCapableBeanFactory fact = ctx.getAutowireCapableBeanFactory();
    if (fact instanceof DefaultListableBeanFactory) {
        DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory)fact;
        for (BeanPostProcessor bpp : dlbf.getBeanPostProcessors()) {
            if (CommonAnnotationBeanPostProcessor.class.isInstance(bpp)) {
                impl.getServerFactory().setBlockPostConstruct(true);
                impl.getServerFactory().setBlockInjection(false);
                return;
            }
            if (bpp instanceof Jsr250BeanPostProcessor) {
                impl.getServerFactory().setBlockInjection(true);
            }
        }
    }
}
 
Example #18
Source File: SpringHibernateDataStore.java    From elide-spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Constructor.</p>
 * Useful for extending the store and relying on existing code
 * to instantiate custom hibernate transaction.
 *
 * @param txManager Spring PlatformTransactionManager
 * @param beanFactory Spring AutowireCapableBeanFactory
 * @param entityManager EntityManager factory
 * @param isScrollEnabled Whether or not scrolling is enabled on driver
 * @param scrollMode Scroll mode to use for scrolling driver
 * @param transactionSupplier Supplier for transaction
 */
protected SpringHibernateDataStore(PlatformTransactionManager txManager,
    AutowireCapableBeanFactory beanFactory,
    EntityManager entityManager,
    ElideProperties elideProperties,
    boolean isScrollEnabled,
    ScrollMode scrollMode,
    HibernateTransactionSupplier transactionSupplier) {
  this.txManager = txManager;
  this.beanFactory = beanFactory;
  this.entityManager = entityManager;
  this.elideProperties = elideProperties;
  this.isScrollEnabled = isScrollEnabled;
  this.scrollMode = scrollMode;
  this.transactionSupplier = transactionSupplier;
}
 
Example #19
Source File: SpecialBeanInstantiationDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
        // 配置 XML 配置文件
        // 启动 Spring 应用上下文
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/special-bean-instantiation-context.xml");
        // 通过 ApplicationContext 获取 AutowireCapableBeanFactory
        AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();

        ServiceLoader<UserFactory> serviceLoader = beanFactory.getBean("userFactoryServiceLoader", ServiceLoader.class);

        displayServiceLoader(serviceLoader);

//        demoServiceLoader();

        // 创建 UserFactory 对象,通过 AutowireCapableBeanFactory
        UserFactory userFactory = beanFactory.createBean(DefaultUserFactory.class);
        System.out.println(userFactory.createUser());

    }
 
Example #20
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowireWithSatisfiedJavaBeanDependency() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("name", "Rod");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("rod", bd);
	assertEquals(1, lbf.getBeanDefinitionCount());
	// Depends on age, name and spouse (TestBean)
	Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertEquals(1, lbf.getBeanDefinitionCount());
	DependenciesBean kerry = (DependenciesBean) registered;
	TestBean rod = (TestBean) lbf.getBean("rod");
	assertSame(rod, kerry.getSpouse());
}
 
Example #21
Source File: ProjectExportServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
private ProjectExportTaskHandle startTask(ProjectExportTask_ImplBase aTask)
{
    ProjectExportTaskHandle handle = aTask.getHandle();
    
    // This autowires the task fields manually.
    AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    factory.autowireBean(aTask);
    factory.initializeBean(aTask, "transientTask");

    tasks.put(handle, new TaskInfo(taskExecutorService.submit(aTask), aTask));
    
    return handle;
}
 
Example #22
Source File: AbstractCrudTestCase.java    From cerberus-source with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void init(final ServletConfig config) throws ServletException {
    super.init(config);
    springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
    final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
    beanFactory.autowireBean(this);
}
 
Example #23
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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));
}
 
Example #24
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example #25
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInvalidAutowireMode() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	try {
		lbf.autowireBeanProperties(new TestBean(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException expected) {
	}
}
 
Example #26
Source File: SqlScriptsTestExecutionListenerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void missingDataSourceAndTxMgr() throws Exception {
	ApplicationContext ctx = mock(ApplicationContext.class);
	given(ctx.getResource(anyString())).willReturn(mock(Resource.class));
	given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class));

	Class<?> clazz = MissingDataSourceAndTxMgr.class;
	BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
	given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
	given(testContext.getApplicationContext()).willReturn(ctx);

	assertExceptionContains("supply at least a DataSource or PlatformTransactionManager");
}
 
Example #27
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertThat(bean.getSpouse(), equalTo(lbf.getBean("test")));
}
 
Example #28
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAutowireExistingBeanByType() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("test", bd);
	DependenciesBean existingBean = new DependenciesBean();
	lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	TestBean test = (TestBean) lbf.getBean("test");
	assertEquals(existingBean.getSpouse(), test);
}
 
Example #29
Source File: AdminMenuJspBeanTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AdminUserDAO getAdminUserDAO( )
{
    AdminUserDAO adminUserDAO = new AdminUserDAO( );
    ApplicationContext context = SpringContextService.getContext( );
    AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory( );
    beanFactory.autowireBean( adminUserDAO );
    return adminUserDAO;
}
 
Example #30
Source File: WicketPageTestBase.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void init()
{
  super.init();
  final ClassPathXmlApplicationContext context = getTestConfiguration().getApplicationContext();
  getComponentInstantiationListeners().add(new SpringComponentInjector(this, context, true));
  getResourceSettings().getStringResourceLoaders().add(new BundleStringResourceLoader(WicketApplication.RESOURCE_BUNDLE_NAME));
  final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
  UserXmlPreferencesCache.setInternalInstance(userXmlPreferencesCache);
}