英文:
Cache Loading in Spring Boot
问题
我有一个缓存类,在应用程序启动之前从 .properties 文件中加载属性,在一个简单的 Java 项目中。它会将所有内容记录下来。我需要将这个项目转换成 Spring Boot 应用程序。
我可以使用哪些注解来实现缓存加载呢?
目前,我将代码编写得像是我的 Spring Boot 应用程序从 @PostConstruct 开始启动,因为我没有使用 web.xml 来加载 servlets。
@RestController
public class ConfigServlet {
@PostConstruct
public void init() {
// 业务逻辑
}
}
并且这个 servlet 会首先启动。那么我该如何在这个过程中加载缓存呢?
应该在这个 servlet 类加载之前就加载缓存。我该如何实现这个概念呢?
英文:
I have a cache class that is loading properties from a .properties file before the application startup, in a simple java project. It logs out everything.I need to convert this project to springboot application.
What annotations can i use to achieve the Cache Loading??
Currently I wrote the code like my spring boot app starts with the @postconstruct , since i am not using web.xml for loading servlets.
@RestController
public class ConfigServlet {
@PostConstruct
public void init() {
//business logic
}
}
And this servlet starts up first. So how can i load cache along this??
It is supposed to load the Cache even before this servlet class loads. How can i achieve this concept??
答案1
得分: 1
假设您的应用程序属性中有以下属性。 您可以按以下方式加载它们。 这些属性将在应用程序启动期间加载。
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
}
英文:
Suppose you have below properties in your application properties. You can load them as below. The properties will be loaded during the application startup.
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论