【问题标题】:Cache Loading in Spring BootSpring Boot 中的缓存加载
【发布时间】:2021-01-07 21:22:14
【问题描述】:

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

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

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

@RestController
public class ConfigServlet {

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

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

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

【问题讨论】:

    标签: java spring spring-boot caching postconstruct


    【解决方案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
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 2016-10-13
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      相关资源
      最近更新 更多