使用@ConfigurationPropertiesSpring Boot,可以轻松地从外部源(尤其是本地配置文件)加载配置。这些文件可以包含自定义的复杂数据结构,因此非常适合我们不想在源代码或数据库中维护的静态数据。
我们的应用程序中需要一些结构化的静态数据。在我们构建了将数据存储在数据库中并允许用户自己维护数据的完整功能之前,静态数据也许是一种解决方法。或者,我们只需要一种方法来轻松维护和访问很少更改的数据,而无需将其存储在数据库中的开销。
用例可能是:
- 维护一个包含结构信息的大型枚举,该信息会不时更改-我们不想在代码中使用枚举,因为我们不想为每个更改重新编译整个应用程序,或者
- 在应用程序中显示静态数据,例如发票抬头中首席执行官的姓名和地址或网页上的“每日报价”,或者
- 使用任何结构化的数据,您可能会想到您不想在代码或数据库中进行维护。
通过其@ConfigurationProperties功能,Spring Boot支持从一个或多个配置文件访问结构化数据。
在本文中,我们将看一下:
- 如何使用数据创建配置文件,
- 如何创建用于验证设置的集成测试,以及
- 如何访问应用程序中的数据。
我们将以“ Quote of the Day”用例为例(实际上,我是在几周前将其构建为向我之前的团队告别的:)。
本文随附GitHub上的示例代码。
将静态数据存储在配置文件中
首先,我们创建一个quotes.yml包含我们的静态数据的YAML文件:
static: quotes: - text: "A clever person solves a problem. A wise person avoids it." author: "Albert Einstein" - text: "Adding manpower to a late software project makes it later." author: "Fred Brooks" |
如果您更喜欢YAML的属性文件,则可以使用它。使用YAML表示嵌套的数据结构更容易。
在我们的例子中,每个引用都有一个文本和一个作者。每个引用将稍后在一个Quote对象中表示。
请注意,我们为数据加上了前缀static:quotes。这是创建唯一名称空间的必要条件,因为Spring Boot稍后将合并此配置文件的内容及其其余配置。
让Spring Boot意识到配置文件
现在,我们必须使Spring Boot知道此配置文件。我们可以通过在spring.config.location每次启动Spring Boot应用程序时设置系统属性来做到这一点:
-Dspring.config.location=./,./quotes.yml
这告诉Spring Boot 在当前文件夹中搜索一个application.properties或application.yml文件(这是默认设置),并另外加载该文件quotes.yml。
这是Spring Boot加载YAML文件并在应用程序中公开内容所需要做的一切。
访问静态数据
首先,我们需要一个Quote数据结构,用作配置数据的容器:
public class Quote { private String text; private String author; public Quote() { } // getters and setters omitted } |
该Quote类只有简单的String属性。如果我们有更复杂的数据类型,则可以使用自定义转换器将配置参数(始终为Strings)转换为自定义类型。
然后,我们利用Spring Boot的@ConfigurationProperties功能将静态数据绑定到QuotesProperties对象:
@Component @ConfigurationProperties("static") public class QuotesProperties { private final List<Quote> quotes; public QuotesProperties(List<Quote> quotes) { this.quotes = quotes; } public List<Quote> getQuotes(){ return this.quotes; } } |
这就是我们的名称空间前缀起作用的地方。将QuotesProperties类绑定到命名空间static和quotes在配置文件结合同名领域前缀。
当配置属性的绑定失败时,Spring Boot在错误消息中有点不透明。您可能会收到错误消息,例如Binding to target ... failed ... property was left unbound不知道根本原因。
就我而言,根本原因始终是我没有在充当配置属性(在本例中为)的数据结构的类之一中提供默认构造函数以及getter和setter方法Quote。Spring Boot需要一个无参数的构造函数以及getter和setter来填充数据。
验证对静态数据的访问
为了测试我们的静态数据是否按预期工作,我们可以创建一个简单的集成测试:
@SpringBootTest( properties = { "spring.config.location = ./,file:./quotes.yml" } ) class QuotesPropertiesTest { @Autowired private QuotesProperties quotesProperties; @Test void staticQuotesAreLoaded() { assertThat(quotesProperties.getQuotes()).hasSize(2); } } |
该测试最重要的部分是设置spring.config.location属性以告诉Spring Boot拾取我们的quotes.yml文件。
然后,我们可以简单地注入QuotesPropertiesbean并断言它包含我们期望的引号。
访问静态数据
在安装了QuotesPropertiesbean并对其进行了测试之后,我们现在可以简单地将其注入任何其他bean中,以执行我们需要的引号。例如,我们可以构建一个调度程序,每5秒记录一次随机报价:
@Configuration @EnableScheduling public class RandomQuotePrinter { private static final Logger logger = LoggerFactory.getLogger(RandomQuotePrinter.class); private final Random random = new Random(); private final QuotesProperties quotesProperties; public RandomQuotePrinter(QuotesProperties quotesProperties) { this.quotesProperties = quotesProperties; } @Scheduled(fixedRate = 5000) void printRandomQuote(){ int index = random.nextInt(quotesProperties.getQuotes().size()); Quote quote = quotesProperties.getQuotes().get(index); logger.info("'{}' - {}", quote.getText(), quote.getAuthor()); } } |
猜你喜欢