简体   繁体   English

Spring 引导缓存加载

[英]Cache Loading in Spring Boot

I have a cache class that is loading properties from a.properties file before the application startup, in a simple java project .我有一个缓存 class,它在应用程序启动之前从 a.properties 文件加载属性,在一个简单的 java 项目中。 It logs out everything.I need to convert this project to springboot application.它会注销所有内容。我需要将此项目转换为 springboot 应用程序。

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.目前我写的代码像我的 spring 启动应用程序以 @postconstruct 开头,因为我没有使用 web.xml 来加载 servlets。

@RestController
public class ConfigServlet {

    @PostConstruct
        public void init() {
    //business logic
    }
}

And this servlet starts up first.这个 servlet 首先启动。 So how can i load cache along this??那么我该如何加载缓存呢?

It is supposed to load the Cache even before this servlet class loads.它应该甚至在此 servlet class 加载之前加载缓存。 How can i achieve this concept??我怎样才能实现这个概念?

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM