在Spring MVC应用程序运行期间加载属性文件

时间:2019-06-05 14:05:19

标签: java spring spring-mvc

我希望能够在部署应用程序后根据需要更改属性,而不是在本地更改属性,然后重新打包和部署该应用程序。

我有以下设置:

@Configuration
@EnableWebMvc
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = {"com.example"})
public class MyApp extends WebMvcConfigurerAdapter {
    @Autowired
    Environment env;
    ...

这很好。但是,如果要更改属性,则必须重新打包并部署应用程序(WAR文件),对吗?我尝试进入爆炸的/tomcat/myapp/WEB-INF/classes/application.properties并在那里进行更改,然后在不重新部署应用程序的情况下重新运行该应用程序,但它似乎仍然具有旧设置。

我想我可以做类似以下的事情,但这似乎不是Spring的做事方式:

Properties loadProps = new Properties();
String value1 = null;
String value2 = null;
try {
    String location = "/path/to/application.properties";
    loadProps.load(new FileInputStream(location));
    value1 = loadProps.getProperty("value1");
    value2 = loadProps.getProperty("value2");
} catch (Exception e) {
    System.out.println("Caught an exception when getting properties file");
    e.printStackTrace();
}

..然后将属性对象加载为Spring bean之类的东西。

关于在Spring MVC中应如何做到这一点的任何建议?

0 个答案:

没有答案