[英]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.