使用 Spring Boot 自动扩展属性

x33g5p2x  于2022-09-25 转载在 Spring  
字(2.6k)|赞(0)|评价(0)|浏览(204)

1.概述

在本文中,我们将通过 Maven Build 方法探索 Spring 提供的属性扩展机制。

2. 配置

在资源文件中,我们可以使用 ${...} 占位符作为变量,在构建时由系统或 maven 属性替换。

默认情况下,maven 资源过滤是不启用的。如果我们从 spring-boot-starter-parent 扩展 Spring Boot 项目,则默认启用资源过滤。在这种情况下,使用@..@ 分隔符代替${},即避免与弹簧式占位符${} 冲突。

如果我们不扩展 spring-boot-starter-parent 而是 importspring-boot-dependencies 那么我们必须自己启用 maven 资源过滤。

对于使用 spring-boot-starter-parent 的 Maven 项目,不需要额外的配置来使用属性扩展:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.7.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

现在我们可以使用* @...@* 占位符来扩展我们项目的属性。这是一个示例,说明我们如何将从 Maven 获取的项目版本保存到我们的属性中:

project-name=@project.name@
app-title=@app.title@
spring-version=@spring.version@

我们只能在匹配这些模式的配置文件中使用这些扩展:

  • */application.yml
  • */application.yaml
  • */application.properties

在没有 spring-boot-starter-parent 父级的情况下,我们需要手动配置此过滤和扩展。我们需要将 resources 元素包含在 pom.xml 文件的 <build> 部分中:

<resources>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/application*.yml</include>
            <include>**/application*.yaml</include>
            <include>**/application*.properties</include>
         </includes>
    </resource>
</resources>

在 <plugins> 中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

现在,邮件类将像这样配置

@SpringBootApplication
public class PropertyExpansionSpringApplication {

    @Bean
    CustomBean customBean() {
        return new CustomBean();
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication bootApp = new SpringApplication(PropertyExpansionSpringApplication.class);
        bootApp.setBannerMode(Banner.Mode.OFF);
        ConfigurableApplicationContext context = bootApp.run(args);
        CustomBean customBean = context.getBean(CustomBean.class);
        customBean.doSomething();
    }

    private static class CustomBean {

        @Value("${project-name}")
        private String projectName;

        @Value("${spring-version}")
        private String springVersion;

        @Value("${app-title}")
        private String appTitle;

        public void doSomething() {
            System.out.printf("Project name: %s%n"
                            + "Spring version: %s%n"
                            + "App title: %s%n",
                    projectName, springVersion, appTitle);
        }
    }

}

输出 :

Project name: Spring-Boot-Tutorial
Spring version: 5.1.9.RELEASE
App title: Application Title

3. 结论

在本快速教程中,我们了解了如何使用 Maven 自动扩展 Spring Boot 属性。一如既往,请在 Github 中找到代码:Click Here

相关文章

微信公众号

最新文章

更多