我需要玩一个属性文件的键。密钥将是动态的,所以我需要下面提到的属性文件的bean作为我当前运行的Spring应用程序。
弹簧配置:
<bean id="multipleWriterLocations" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:writerLocations.properties</value>
<value>file:config/writerLocations.properties</value>
</list>
</property>
</bean>
Java代码:
Properties prop = appContext.getBean("multipleWriterLocations")
我需要在Spring-boot中使用相同的Properties bean实例。我需要将现有的Spring应用程序转换为Spring-Boot而不改变功能。
使用@PropertySource()获取属性文件值的一种方法,但在这种情况下我需要键名。但就我而言,密钥名称是未知的,我需要从Properties bean中获取keySet。
答案 0 :(得分:0)
您可以使用@ImportResource("classpath:config.xml")
config.xml
包含上方PropertiesFactoryBean
的{{1}},然后将其自动装入您的@SpringBootApplication
或@Configuration
课程。
@SpringBootApplication
@ImportResource("classpath:config.xml")
public class App {
public App(PropertiesFactoryBean multipleWriterLocations) throws IOException {
// Access the Properties populated from writerLocations.properties
Properties properties = multipleWriterLocations.getObject();
System.out.println(properties);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}