问题描述
我有一个缓存类,它在应用程序启动前的简单Java项目中从.properties文件加载属性。它注销了所有内容。我需要将此项目转换为springboot应用程序。
我可以使用哪些注释来实现缓存加载?
由于我没有使用web.xml来加载servlet,所以我现在写的代码就像我的spring boot app以@postconstruct开头。
@RestController
public class ConfigServlet {
@postconstruct
public void init() {
//business logic
}
}
然后该servlet首先启动。那么我如何沿此加载缓存?
应该甚至在加载此Servlet类之前加载Cache。我如何实现这个概念?
解决方法
假设您的应用程序属性中具有以下属性。您可以按以下方式加载它们。这些属性将在应用程序启动期间加载。
application.properties
test.a = 10
test.b =20
test1.a = 30
test1.b = 40
@Configuration
@ConfigurationProperties
Class CacheProperties {
Map<String,String> test;
Map<String,String> test1;
public String getTestB() {
return test.get("b");
}
public String getTestA() {
return test.get("a");
}
public String getTest1B() {
return test1.get("b");
}
public String getTest1A() {
return test1.get("a");
}
//setters
}