首页 > 解决方案 > Spring Boot 中的缓存加载

问题描述

我有一个缓存类,它在应用程序启动之前从 .properties 文件加载属性,在一个简单的 java 项目中。它会注销所有内容。我需要将此项目转换为 springboot 应用程序。

我可以使用哪些注释来实现缓存加载?

目前我编写的代码就像我的 spring boot 应用程序以 @postconstruct 开头一样,因为我没有使用 web.xml 来加载 servlet。

@RestController
public class ConfigServlet {

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

这个 servlet 首先启动。那么我该如何加载缓存呢?

它甚至应该在加载这个 servlet 类之前加载缓存。我怎样才能实现这个概念?

标签: javaspringspring-bootcachingpostconstruct

解决方案


假设您的应用程序属性中有以下属性。您可以按如下方式加载它们。属性将在应用程序启动期间加载。

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


}

推荐阅读