简体   繁体   English

如何在 application.properties 文件中覆盖 spring boot starter 的默认属性?

[英]How to override spring boot starter's default properties in application.properties file?

I created a multi module maven project that contains the library module (spring boot starter application) and application module (spring boot application that have included library as a dependency).我创建了一个多模块 maven 项目,其中包含library模块(spring boot starter 应用程序)和application模块(spring boot 应用程序已包含library作为依赖项)。

This is the structure of my project:这是我的项目结构:

.
├── application
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── kotlin
│       │   │   └── com
│       │   │       └── application
│       │   │           ├── ApplicationService.kt
│       │   │           └── Application.kt
│       │   └── resources
│       │       └── application.properties
│       └── test
│           └── kotlin
│               └── com
│                   └── application
│                       └── ApplicationServiceTest.kt
├── library
│   ├── pom.xml
│   └── src
│       └── main
│           ├── kotlin
│           │   └── com
│           │       └── application
│           │           ├── LibraryService.kt
│           │           └── Properties.kt
│           └── resources
│               ├── META-INF
│               │   └── spring.factories
│               └── config
│                   └── application.properties
└── pom.xml

library/.../Properties.kt:图书馆/.../Properties.kt:

@ConfigurationProperties("properties")
class Properties {
    lateinit var name: String
}

library/.../LibraryService.kt:图书馆/.../LibraryService.kt:

@Service
@EnableConfigurationProperties(Properties::class)
class LibraryService(private val properties: Properties) {
    fun name() = properties.name
}

library/.../spring.factories:图书馆/.../spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.application.LibraryService

library/.../config/application.properties:库/.../config/application.properties:

properties.name=library

application/.../Application.kt应用程序/.../Application.kt

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

application/.../ApplicationService.kt应用程序/.../ApplicationService.kt

@Service
class ApplicationService(private val libraryService: LibraryService) {
    fun call() = libraryService.name()
}

application/.../application.properties应用程序/.../application.properties

properties.name=application

So, I have the library module where I put application.properties file with default parameter properties.name=library .所以,我有一个library模块,我将application.properties文件放置在默认参数properties.name=library The library module has Property class injected in LibraryService . library模块在LibraryService注入了Property类。 LibraryService has the simple method name() that just returns value from property. LibraryService有一个简单的方法name()LibraryService从属性返回值。 I also have application module where I use LibraryService in ApplicationService and invoke name() function, but I have application.properties in application module where properties.name=application .我也有application模块,我在ApplicationService使用LibraryService并调用name()函数,但我在application模块中有application.properties ,其中properties.name=application

I expect that application's properties.name=application overrides library's properties.name=library and ApplicationService::call must return value application instead of default value library in properties.name in library module .我希望application's properties.name=application覆盖library's properties.name=library并且ApplicationService::call必须返回值application而不是properties.name in library module的默认值library But this does not happen.但这不会发生。 ApplicationService::call returns value library . ApplicationService::call返回值library

I created simple junit test to reproduce this behaviour ( ApplicationServiceTest.kt ):我创建了简单的 junit 测试来重现这种行为( ApplicationServiceTest.kt ):

@SpringBootTest
class ApplicationServiceTest {

    @Autowired
    lateinit var applicationService: ApplicationService

    @Test
    fun test() {
        println(applicationService.call())
    }
}

It prints library .它打印library I would like to have the following behaviour: library has some several defined default properties, but I want to be able to override some of these properties in application .我想有以下行为: library有一些定义的默认属性,但我希望能够在application覆盖其中的一些属性。 How to achieve that?如何做到这一点?

source code: https://github.com/grolegor/maven-multi-module-spring-starter源代码: https : //github.com/grolegor/maven-multi-module-spring-starter

Form the documentation形成文档

4.2.3. 4.2.3. Application Property Files SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:应用程序属性文件 SpringApplication 从以下位置的 application.properties 文件加载属性并将它们添加到 Spring Environment:

1 . 1 . A /config subdirectory of the current directory当前目录的 /config 子目录

2 . 2 . The current directory当前目录

3 . 3 . A classpath /config package一个类路径 /config 包

4 . 4 . The classpath root类路径根

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations ).该列表按优先级排序(在列表中较高位置定义的属性覆盖在较低位置定义的属性)。

So in your case [library] config/application.properties will be used, because it is higher-ordered then [application] application.properties .因此,在您的情况下,将使用[library] config/application.properties ,因为它比[application] application.properties的顺序更高。

Also, you cannot use application.properties twice.此外,您不能使用application.properties两次。

Looking in your repository I would suggest that you remove the /config/application.properties from the library-module and provide default values in your Properties -class查看您的存储库,我建议您从库模块中删除/config/application.properties并在Properties -class 中提供默认值

package com.application

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties("properties")
class Properties {
    var name: String = "library"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties 如何在 Spring-Boot 的生产过程中覆盖 application.properties? - How to override application.properties during production in Spring-Boot? 如何在Spring Boot集成测试中覆盖application.properties? - How to override application.properties in Spring Boot integration test? 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? 为什么 Spring 引导不从默认的 application.properties 文件中读取 - Why is Spring Boot not reading from default application.properties file Spring boot application.properties 文件读取 - Spring boot application.properties file reading Spring 引导无法识别 application.properties 文件 - Spring Boot not recognizing application.properties file Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM