java - Spring Boot 中的缓存加载
问题描述
我有一个缓存类,它在应用程序启动之前从 .properties 文件加载属性,在一个简单的 java 项目中。它会注销所有内容。我需要将此项目转换为 springboot 应用程序。
我可以使用哪些注释来实现缓存加载?
目前我编写的代码就像我的 spring boot 应用程序以 @postconstruct 开头一样,因为我没有使用 web.xml 来加载 servlet。
@RestController
public class ConfigServlet {
@PostConstruct
public void init() {
//business logic
}
}
这个 servlet 首先启动。那么我该如何加载缓存呢?
它甚至应该在加载这个 servlet 类之前加载缓存。我怎样才能实现这个概念?
解决方案
假设您的应用程序属性中有以下属性。您可以按如下方式加载它们。属性将在应用程序启动期间加载。
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
}
推荐阅读
- lua - 在 Lua 中被玩家踩到时减小对象大小
- c++ - 错误 'CDC::CDC(const CDC &)': 试图引用已删除的函数 MFCBreakout
- html - 背景图像上的透明彩色覆盖
- python - 将 Angular 与 django 集成时由 vendor.js 启动的文件出现 404 错误
- angular - 突出显示 html 模板中的角度语法
- reporting-services - 即使详细信息为空,SSRS 2008 也会显示行
- c# - 如何将标题应用于文字处理文档中的所有段落?
- azure - 安装 MS Service Fabric SDK 3.2.162 时出现问题
- schema - Pentaho Schema Workbench 维度属性
- c# - UWP XAML 中的 YouTube 样式搜索结果