Java Code Examples for org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#setProperties()

The following examples show how to use org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#setProperties() . 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: FormattingConversionServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception {
	GenericApplicationContext context = new GenericApplicationContext();
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("dateStyle", "S-");
	props.setProperty("datePattern", "M-d-yy");
	ppc.setProperties(props);
	context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	context.getBeanFactory().registerSingleton("ppc", ppc);
	context.refresh();
	formattingService = context.getBean("formattingService", FormattingConversionService.class);
	doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
 
Example 2
Source File: JsHttpRequestTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void additionalSpringConfiguration(GenericApplicationContext applicationContext) throws Exception {
    // bring in some property values from a Properties file
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    Properties properties = new Properties();
    properties.setProperty("staticResourceURL", getStaticResourceURL());
    cfg.setProperties(properties);
    // now actually do the replacement
    cfg.postProcessBeanFactory(applicationContext.getBeanFactory());

}
 
Example 3
Source File: CoreAppConfig.java    From logsniffer with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a general properties placeholder configurer based on
 * {@link #logSnifferProperties()}.
 * 
 * @param props
 *            autowired logSnifferProperties bean
 * @return A general properties placeholder configurer.
 * @throws IOException
 */
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@Autowired
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(
		@Qualifier(BEAN_LOGSNIFFER_PROPS) final Properties props) throws IOException {
	final PropertyPlaceholderConfigurer c = new PropertyPlaceholderConfigurer();
	c.setIgnoreResourceNotFound(true);
	c.setIgnoreUnresolvablePlaceholders(true);
	c.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
	c.setProperties(props);
	return c;
}
 
Example 4
Source File: PropertyFileConfig.java    From micro-server with Apache License 2.0 5 votes vote down vote up
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {

    PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
    Properties props = propertyFactory();

    configurer.setProperties(props);
    configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
    return configurer;
}
 
Example 5
Source File: FormattingConversionServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception {
	GenericApplicationContext context = new GenericApplicationContext();
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("dateStyle", "S-");
	props.setProperty("datePattern", "M-d-yy");
	ppc.setProperties(props);
	context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	context.getBeanFactory().registerSingleton("ppc", ppc);
	context.refresh();
	formattingService = context.getBean("formattingService", FormattingConversionService.class);
	doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
 
Example 6
Source File: FormattingConversionServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void testFormatFieldForAnnotationWithPlaceholders() throws Exception {
	GenericApplicationContext context = new GenericApplicationContext();
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("dateStyle", "S-");
	props.setProperty("datePattern", "M-d-yy");
	ppc.setProperties(props);
	context.getBeanFactory().registerSingleton("ppc", ppc);
	context.refresh();
	context.getBeanFactory().initializeBean(formattingService, "formattingService");
	formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
	doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
 
Example 7
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourceInjectionWithResolvableDependencyType() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
	tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean4", tbd);

	bf.registerResolvableDependency(BeanFactory.class, bf);
	bf.registerResolvableDependency(INestedTestBean.class, new ObjectFactory<Object>() {
		@Override
		public Object getObject() throws BeansException {
			return new NestedTestBean();
		}
	});

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean4");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	INestedTestBean tb = bean.getTestBean6();
	assertNotNull(tb);

	ExtendedResourceInjectionBean anotherBean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean6(), tb);

	String[] depBeans = bf.getDependenciesForBean("annotatedBean");
	assertEquals(1, depBeans.length);
	assertEquals("testBean4", depBeans[0]);
}
 
Example 8
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResourceInjectionWithResolvableDependencyType() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
	tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean4", tbd);

	bf.registerResolvableDependency(BeanFactory.class, bf);
	bf.registerResolvableDependency(INestedTestBean.class, new ObjectFactory<Object>() {
		@Override
		public Object getObject() throws BeansException {
			return new NestedTestBean();
		}
	});

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean4");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	INestedTestBean tb = bean.getTestBean6();
	assertNotNull(tb);

	ExtendedResourceInjectionBean anotherBean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean6(), tb);

	String[] depBeans = bf.getDependenciesForBean("annotatedBean");
	assertEquals(1, depBeans.length);
	assertEquals("testBean4", depBeans[0]);
}
 
Example 9
Source File: FormattingConversionServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void formatFieldForAnnotationWithPlaceholders() throws Exception {
	GenericApplicationContext context = new GenericApplicationContext();
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("dateStyle", "S-");
	props.setProperty("datePattern", "M-d-yy");
	ppc.setProperties(props);
	context.getBeanFactory().registerSingleton("ppc", ppc);
	context.refresh();
	context.getBeanFactory().initializeBean(formattingService, "formattingService");
	formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
	doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
 
Example 10
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testResourceInjectionWithResolvableDependencyType() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
	tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean4", tbd);

	bf.registerResolvableDependency(BeanFactory.class, bf);
	bf.registerResolvableDependency(INestedTestBean.class, new ObjectFactory<Object>() {
		@Override
		public Object getObject() throws BeansException {
			return new NestedTestBean();
		}
	});

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean4");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	INestedTestBean tb = bean.getTestBean6();
	assertNotNull(tb);

	ExtendedResourceInjectionBean anotherBean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean6(), tb);

	String[] depBeans = bf.getDependenciesForBean("annotatedBean");
	assertEquals(1, depBeans.length);
	assertEquals("testBean4", depBeans[0]);
}
 
Example 11
Source File: FormattingConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception {
	GenericApplicationContext context = new GenericApplicationContext();
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("dateStyle", "S-");
	props.setProperty("datePattern", "M-d-yy");
	ppc.setProperties(props);
	context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	context.getBeanFactory().registerSingleton("ppc", ppc);
	context.refresh();
	formattingService = context.getBean("formattingService", FormattingConversionService.class);
	doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
 
Example 12
Source File: FormattingConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void formatFieldForAnnotationWithPlaceholders() throws Exception {
	GenericApplicationContext context = new GenericApplicationContext();
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("dateStyle", "S-");
	props.setProperty("datePattern", "M-d-yy");
	ppc.setProperties(props);
	context.getBeanFactory().registerSingleton("ppc", ppc);
	context.refresh();
	context.getBeanFactory().initializeBean(formattingService, "formattingService");
	formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
	doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false);
}
 
Example 13
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testExtendedResourceInjectionWithOverriding() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	TestBean tb5 = new TestBean();
	annotatedBd.getPropertyValues().add("testBean2", tb5);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb5, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	try {
		bf.getBean("annotatedBean2");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
		NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
		assertEquals("testBean9", innerEx.getBeanName());
	}

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 14
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testExtendedResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("value", "5");
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
	assertSame(tb6, bean2.testBean);

	ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
	assertSame(5, bean3.value);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 15
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExtendedResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("value", "5");
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
	assertSame(tb6, bean2.testBean);

	ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
	assertSame(5, bean3.value);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 16
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExtendedResourceInjectionWithOverriding() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	TestBean tb5 = new TestBean();
	annotatedBd.getPropertyValues().add("testBean2", tb5);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb5, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	try {
		bf.getBean("annotatedBean2");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
		NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
		assertEquals("testBean9", innerEx.getBeanName());
	}

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 17
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testExtendedResourceInjectionWithOverriding() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	TestBean tb5 = new TestBean();
	annotatedBd.getPropertyValues().add("testBean2", tb5);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb5, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	try {
		bf.getBean("annotatedBean2");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
		NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
		assertEquals("testBean9", innerEx.getBeanName());
	}

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 18
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testExtendedResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("value", "5");
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
	assertSame(tb6, bean2.testBean);

	ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
	assertSame(5, bean3.value);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 19
Source File: SpringContextSupport.java    From spring-boot-akka-event-sourcing-starter with Apache License 2.0 3 votes vote down vote up
private PropertyPlaceholderConfigurer propertyPlaceholderConfigurerWith(Properties userProvidedProperties) {

		PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
		configurer.setProperties(mergeWithDefault(userProvidedProperties));
		return configurer;

	}