下面列出了org.springframework.beans.factory.config.BeanExpressionContext#org.springframework.beans.factory.config.PropertiesFactoryBean 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void byTypeAutowireWithPrimaryInParentFactory() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void byTypeAutowireWithPrimaryOverridingParentFactory() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
propsDef.setPrimary(true);
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
propsDef.setPrimary(true);
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void testExpressionInStringArray() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
given(beanExpressionResolver.evaluate(eq("#{foo}"), any(BeanExpressionContext.class)))
.willReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
bf.setBeanExpressionResolver(beanExpressionResolver);
RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("locations", new String[]{"#{foo}"});
rbd.setPropertyValues(pvs);
bf.registerBeanDefinition("myProperties", rbd);
Properties properties = (Properties) bf.getBean("myProperties");
assertEquals("bar", properties.getProperty("foo"));
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
Properties result = propertiesFactoryBean.getObject();
// 从环境信息中剥离出配置好的环境信息,覆盖默认的配置
String dsName = result.getProperty("org.quartz.jobStore.dataSource");
if (StringUtils.isEmpty(dsName)) {
throw new RuntimeException("quartz配置文件错误,org.quartz.jobStore.dataSource不能为空!");
}
result.setProperty("org.quartz.dataSource." + dsName + ".URL", jdbcUrl);
result.setProperty("org.quartz.dataSource." + dsName + ".user", jdbcUserName);
result.setProperty("org.quartz.dataSource." + dsName + ".password", jdbcPassword);
return result;
}
/**
* <p>描述:定义bean并加装多个自定义属性文件</p>
* @param properties
* @return
* @author LZC
*/
@Bean("props")
public PropertiesFactoryBean propertiesFactoryBean(@Qualifier("prop")Properties properties) {
PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
//加载一个属性文件
factoryBean.setProperties(properties);
/*
* 用来加装多个属性文件,其中属性名称相同的,后加载的会覆盖之前加载的
*/
// Properties properties1 = new Properties();
// Properties properties2 = new Properties();
// try {
// properties1.load(this.getClass().getClassLoader().getResourceAsStream("externals/app.properties"));
// properties2.load(this.getClass().getClassLoader().getResourceAsStream("externals.properties"));
// } catch (IOException e) {
// e.printStackTrace();
// }
// factoryBean.setPropertiesArray(properties1, properties2);
return factoryBean;
}
@Test
public void byTypeAutowireWithPrimaryInParentFactory() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void byTypeAutowireWithPrimaryOverridingParentFactory() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
propsDef.setPrimary(true);
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
propsDef.setPrimary(true);
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void testExpressionInStringArray() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.any(BeanExpressionContext.class)))
.thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
bf.setBeanExpressionResolver(beanExpressionResolver);
RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("locations", new String[]{"#{foo}"});
rbd.setPropertyValues(pvs);
bf.registerBeanDefinition("myProperties", rbd);
Properties properties = (Properties) bf.getBean("myProperties");
assertEquals("bar", properties.getProperty("foo"));
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
//获取配置属性
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
//创建SchedulerFactoryBean
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setQuartzProperties(propertiesFactoryBean.getObject());
//使用数据源,自定义数据源
factory.setJobFactory(jobFactory);
factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时,会等待所有已经启动的quartz job结束后spring才能完全shutdown。
factory.setOverwriteExistingJobs(false);
factory.setStartupDelay(1);
return factory;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
//获取配置属性
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
//创建SchedulerFactoryBean
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setQuartzProperties(propertiesFactoryBean.getObject());
//使用数据源,自定义数据源
factory.setJobFactory(jobFactory);
factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时,会等待所有已经启动的quartz job结束后spring才能完全shutdown。
factory.setOverwriteExistingJobs(false);
factory.setStartupDelay(1);
return factory;
}
@Test
public void byTypeAutowireWithPrimaryInParentFactory() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void byTypeAutowireWithPrimaryOverridingParentFactory() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
propsDef.setPrimary(true);
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
CountingFactory.reset();
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
propsDef.getPropertyValues().add("properties", "name=props3");
propsDef.setPrimary(true);
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
@Test
public void testExpressionInStringArray() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
when(beanExpressionResolver.evaluate(eq("#{foo}"), Matchers.any(BeanExpressionContext.class)))
.thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
bf.setBeanExpressionResolver(beanExpressionResolver);
RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("locations", new String[]{"#{foo}"});
rbd.setPropertyValues(pvs);
bf.registerBeanDefinition("myProperties", rbd);
Properties properties = (Properties) bf.getBean("myProperties");
assertEquals("bar", properties.getProperty("foo"));
}
@Bean
public Properties propertyFactory() throws IOException {
List<Resource> resources = loadPropertyResource();
PropertiesFactoryBean factory = new PropertiesFactoryBean();
factory.setLocations(resources.toArray(new Resource[resources.size()]));
factory.afterPropertiesSet();
Properties props = factory.getObject();
new ConfigAccessor().get()
.getProperties()
.stream()
.forEach(e -> {
if (props.getProperty(e._1()) == null) {
props.put(e._1(), e._2());
}
});
System.getProperties()
.entrySet()
.forEach(e -> props.put(e.getKey(), e.getValue()));
return props;
}
@Bean
public static SSLProperties sslProperties() throws IOException {
PropertiesFactoryBean factory = new PropertiesFactoryBean();
URL url = SSLConfig.class.getClassLoader().getResource("ssl.properties");
if (url != null) {
Resource reource = new UrlResource(url);
factory.setLocation(reource);
factory.afterPropertiesSet();
Properties properties = factory.getObject();
return SSLProperties.builder()
.keyStoreFile(properties.getProperty(keyStoreFile))
.keyStorePass(properties.getProperty(keyStorePass))
.trustStoreFile(properties.getProperty(trustStoreFile))
.trustStorePass(properties.getProperty(trustStorePass))
.keyStoreType(properties.getProperty(keyStoreType))
.keyStoreProvider(properties.getProperty(keyStoreProvider))
.trustStoreType(properties.getProperty(trustStoreType))
.trustStoreProvider(properties.getProperty(trustStoreProvider))
.clientAuth(properties.getProperty(clientAuth))
.ciphers(properties.getProperty(ciphers))
.protocol(properties.getProperty(protocol)).build();
}
return null;
}
@Bean(name = { BEAN_LOGSNIFFER_PROPS })
@Autowired
public PropertiesFactoryBean logSnifferProperties(final ApplicationContext ctx) throws IOException {
if (ctx.getEnvironment().acceptsProfiles("!" + ContextProvider.PROFILE_NONE_QA)) {
final File qaFile = File.createTempFile("logsniffer", "qa");
qaFile.delete();
final String qaHomeDir = qaFile.getPath();
logger.info("QA mode active, setting random home directory: {}", qaHomeDir);
System.setProperty("logsniffer.home", qaHomeDir);
}
final PathMatchingResourcePatternResolver pathMatcher = new PathMatchingResourcePatternResolver();
Resource[] classPathProperties = pathMatcher.getResources("classpath*:/config/**/logsniffer-*.properties");
final Resource[] metainfProperties = pathMatcher
.getResources("classpath*:/META-INF/**/logsniffer-*.properties");
final PropertiesFactoryBean p = new PropertiesFactoryBean();
for (final Resource r : metainfProperties) {
classPathProperties = (Resource[]) ArrayUtils.add(classPathProperties, r);
}
classPathProperties = (Resource[]) ArrayUtils.add(classPathProperties,
new FileSystemResource(System.getProperty("logsniffer.home") + "/" + LOGSNIFFER_PROPERTIES_FILE));
p.setLocations(classPathProperties);
p.setProperties(System.getProperties());
p.setLocalOverride(true);
p.setIgnoreResourceNotFound(true);
return p;
}
private static Properties loadProperties(Resource... propertyResources) throws IOException
{
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setFileEncoding(StandardCharsets.UTF_8.name());
propertiesFactoryBean.setLocations(propertyResources);
propertiesFactoryBean.setSingleton(false);
return propertiesFactoryBean.getObject();
}
@Test
public void testEvents() {
ComponentDefinition propertiesComponent = this.listener.getComponentDefinition("myProperties");
assertNotNull("Event for 'myProperties' not sent", propertiesComponent);
AbstractBeanDefinition propertiesBean = (AbstractBeanDefinition) propertiesComponent.getBeanDefinitions()[0];
assertEquals("Incorrect BeanDefinition", PropertiesFactoryBean.class, propertiesBean.getBeanClass());
ComponentDefinition constantComponent = this.listener.getComponentDefinition("min");
assertNotNull("Event for 'min' not sent", propertiesComponent);
AbstractBeanDefinition constantBean = (AbstractBeanDefinition) constantComponent.getBeanDefinitions()[0];
assertEquals("Incorrect BeanDefinition", FieldRetrievingFactoryBean.class, constantBean.getBeanClass());
}
@Test
public void testRegisterExistingSingletonWithNameOverriding() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("test.(class)", TestBean.class.getName());
p.setProperty("test.name", "Tony");
p.setProperty("test.age", "48");
p.setProperty("test.spouse(ref)", "singletonObject");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
lbf.registerBeanDefinition("singletonObject", new RootBeanDefinition(PropertiesFactoryBean.class));
Object singletonObject = new TestBean();
lbf.registerSingleton("singletonObject", singletonObject);
lbf.preInstantiateSingletons();
assertTrue(lbf.isSingleton("singletonObject"));
assertEquals(TestBean.class, lbf.getType("singletonObject"));
TestBean test = (TestBean) lbf.getBean("test");
assertEquals(singletonObject, lbf.getBean("singletonObject"));
assertEquals(singletonObject, test.getSpouse());
Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true);
assertEquals(2, beansOfType.size());
assertTrue(beansOfType.containsValue(test));
assertTrue(beansOfType.containsValue(singletonObject));
beansOfType = lbf.getBeansOfType(null, false, true);
Iterator<String> beanNames = lbf.getBeanNamesIterator();
assertEquals("test", beanNames.next());
assertEquals("singletonObject", beanNames.next());
assertFalse(beanNames.hasNext());
assertEquals(2, beansOfType.size());
assertTrue(lbf.containsSingleton("test"));
assertTrue(lbf.containsSingleton("singletonObject"));
assertTrue(lbf.containsBeanDefinition("test"));
assertTrue(lbf.containsBeanDefinition("singletonObject"));
}
/**
* 加载配置属性
*
* @return
* @throws IOException
*/
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/spring-quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Test
public void testEvents() {
ComponentDefinition propertiesComponent = this.listener.getComponentDefinition("myProperties");
assertNotNull("Event for 'myProperties' not sent", propertiesComponent);
AbstractBeanDefinition propertiesBean = (AbstractBeanDefinition) propertiesComponent.getBeanDefinitions()[0];
assertEquals("Incorrect BeanDefinition", PropertiesFactoryBean.class, propertiesBean.getBeanClass());
ComponentDefinition constantComponent = this.listener.getComponentDefinition("min");
assertNotNull("Event for 'min' not sent", propertiesComponent);
AbstractBeanDefinition constantBean = (AbstractBeanDefinition) constantComponent.getBeanDefinitions()[0];
assertEquals("Incorrect BeanDefinition", FieldRetrievingFactoryBean.class, constantBean.getBeanClass());
}
@Test
public void testRegisterExistingSingletonWithNameOverriding() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("test.(class)", TestBean.class.getName());
p.setProperty("test.name", "Tony");
p.setProperty("test.age", "48");
p.setProperty("test.spouse(ref)", "singletonObject");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
lbf.registerBeanDefinition("singletonObject", new RootBeanDefinition(PropertiesFactoryBean.class));
Object singletonObject = new TestBean();
lbf.registerSingleton("singletonObject", singletonObject);
lbf.preInstantiateSingletons();
assertTrue(lbf.isSingleton("singletonObject"));
assertEquals(TestBean.class, lbf.getType("singletonObject"));
TestBean test = (TestBean) lbf.getBean("test");
assertEquals(singletonObject, lbf.getBean("singletonObject"));
assertEquals(singletonObject, test.getSpouse());
Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true);
assertEquals(2, beansOfType.size());
assertTrue(beansOfType.containsValue(test));
assertTrue(beansOfType.containsValue(singletonObject));
beansOfType = lbf.getBeansOfType(null, false, true);
Iterator<String> beanNames = lbf.getBeanNamesIterator();
assertEquals("test", beanNames.next());
assertEquals("singletonObject", beanNames.next());
assertFalse(beanNames.hasNext());
assertEquals(2, beansOfType.size());
assertTrue(lbf.containsSingleton("test"));
assertTrue(lbf.containsSingleton("singletonObject"));
assertTrue(lbf.containsBeanDefinition("test"));
assertTrue(lbf.containsBeanDefinition("singletonObject"));
}
@Bean
public PropertiesFactoryBean configProperties() throws Exception{
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
propertiesFactoryBean.setLocations(resolver.getResources("classpath*:application.properties"));
return propertiesFactoryBean;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
/**
* 初始化quartz配置
*
* @return
* @throws IOException
*/
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/config/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}