[英]How can I get the bean of a property file in Spring-Boot?
I need to play around keys of a property file. 我需要玩弄属性文件的键。 Key will be dynamic, so I need the bean of property file as mentioned below as my current running Spring application.
密钥将是动态的,因此我需要如下所述的属性文件bean作为我当前正在运行的Spring应用程序。
Spring Configuration: 弹簧配置:
<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 Code: Java代码:
Properties prop = appContext.getBean("multipleWriterLocations")
I need the same Properties bean instance in Spring-boot. 我在Spring-boot中需要相同的Properties Bean实例。 I need to transform existing Spring application into Spring-Boot without changing the functionalities.
我需要在不更改功能的情况下将现有的Spring应用程序转换为Spring-Boot。
One way to get properties file value using @PropertySource(), but in this case I need the key name. 一种使用@PropertySource()获取属性文件值的方法,但是在这种情况下,我需要键名。 But in my case, key name is not known and I need to fetch keySet from the Properties bean.
但就我而言,键名未知,我需要从Properties Bean中获取keySet。
You can use @ImportResource("classpath:config.xml")
where config.xml
contains your PropertiesFactoryBean
above, and then autowire it into your @SpringBootApplication
or @Configuration
class. 您可以使用
@ImportResource("classpath:config.xml")
,其中config.xml
包含上面的PropertiesFactoryBean
,然后将其自动连接到@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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.