在我的Spring Boot应用程序中,我想要将属性外部化以在Docker容器中运行.首次部署时,my-server/src/main/resources/application.yml应用程序按预期加载和使用当前所属的属性.一切正常.
但是,我的问题是我需要这些属性可以根据需要更新,所以我需要application.yml在Docker容器上访问一次该文件.但此时,它build/docker/在运行buildDocker任务之前未包含在目录中,因此在首次部署后不会复制或访问.
所以,我试过的是将Yaml文件复制到docker/构建目录,将其复制到可访问的目录(/opt/meanwhileinhell/myapp/conf),并使用该spring.config.location属性将配置的位置传递给我的Dockerfile中的Jar:
ENTRYPOINT ["java",\
...
"-jar", "/app.jar",\
"--spring.config.location=classpath:${configDirectory}"]
Run Code Online (Sandbox Code Playgroud)
看看在Docker容器上运行的Command我可以看到这是预期的:
/app.jar --spring.config.location=classpath:/opt/meanwhileinhell/myapp/conf]
Run Code Online (Sandbox Code Playgroud)
但是,当我更新此文件中的属性并重新启动Docker容器时,它没有获取更改.文件权限是:
-rw-r--r-- 1 root root 618 Sep 5 13:59 application.yml
Run Code Online (Sandbox Code Playgroud)
该文件规定:
配置自定义配置位置时,除默认位置外,还会使用它们.在默认位置之前搜索自定义位置.
我似乎无法弄清楚我做错了什么或错误解释,但更重要的是,这是将这种类型的Docker场景的配置外部化的正确方法吗?
在最终总结之前,我发现了许多问题和教程.想要记录它,以便其他人可以节省许多小时的挫折.
我试图在BitBucket上获得一个私有git存储库,以使用部署密钥与Spring Boot Config Server一起工作,并让它在Docker中运行.我遇到了很多问题.
我似乎无法弄清楚我应该把SSH信息放在哪里.所有教程似乎都适用于https.
我一直收到私钥无效的错误.
似乎有几种方法可以完成这项工作,但只有一种方法可以帮助我.
git ssh-keys docker spring-cloud-config spring-boot-configuration
给定一些具有无法解析的占位符的应用程序配置,如下所示 application.yml
my:
thing: ${missing-placeholder}/whatever
Run Code Online (Sandbox Code Playgroud)
使用@Value批注时,将验证配置文件中的占位符,因此在这种情况下:
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropValues {
@Value("${my.thing}") String thing;
public String getThing() { return thing; }
}
Run Code Online (Sandbox Code Playgroud)
我得到一个IllegalArgumentException: Could not resolve placeholder 'missing-placeholder' in value "${missing-placeholder}/whatever"。这是因为该值是直接由设置的AbstractBeanFactory.resolveEmbeddedValue,没有任何东西可以捕获由抛出的异常PropertyPlaceholderHelper.parseStringValue
但是,在寻找@ConfigurationProperties样式时,我注意到缺少此验证,例如在这种情况下:
package com.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@ConfigurationProperties(prefix = "my")
public class Props {
private String thing;
public String getThing() { return thing; }
public void setThing(String thing) { this.thing = thing; }
} …Run Code Online (Sandbox Code Playgroud) validation configuration spring spring-boot spring-boot-configuration
在Spring Boot应用程序中,我想使用@ConfigurationProperties具有相同前缀的注释来根据配置文件配置我的两个数据源.为什么Spring Boot配置处理器禁止它?gradle报告的错误是:
...
:compileJava ... error: Duplicate `@ConfigurationProperties` definition for prefix 'spring.datasource'
Run Code Online (Sandbox Code Playgroud)
笔记:
When using @ConfigurationProperties it is recommended to add 'spring-boot-configuration-processor' to your classpath to generate configuration metadata出现警告)的build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile 'org.springframework.boot:spring-boot-configuration-processor:1.5.4.RELEASE' …Run Code Online (Sandbox Code Playgroud) 我试图让Intellij使用gradle来识别我的属性。我已经按照这里的步骤进行了。所以这意味着我有一个@ConfigurationProperties带有某些属性的带注释的类。
我添加了spring依赖项来处理它们:
dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"
}
compileJava.dependsOn(processResources)
Run Code Online (Sandbox Code Playgroud)
我添加了插件(我尝试不使用插件,而只是使其成为编译依赖项,没有变化)
buildscript {
repositories {
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies { classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE' }
}
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
Run Code Online (Sandbox Code Playgroud)
运行构建时,我看到build/classes/java/main/META-INF/spring-configuration-metadata.json根据属性创建了一个文件。当我尝试在application.yml或中使用该属性时application.properties,Intellij说它无法解析。
文档确实说应该调用additional-spring-configuration-metadata.json它,并且可能期望它能够被调用以对其进行处理,但是我没有看到这样的方式来使内部版本命名,也没有配置Intellij以其他方式期望它。
有没有人用gradle来工作?还是这是一个错误。
编辑我创建了一个带有两个项目的仓库来演示这一点。一种用于gradle,另一种用于maven。我从start.spring.io创建了项目,基本上只添加了属性配置。在这两种情况下,我还使用了直接编译依赖项,而不是可选的/ compileOnly。
我之前没有确认这一点,但是代码助手确实适用于Maven,但不适用于gradle。双方建立spring-configuration-metadata.json在META-INF在各自构建文件夹。我不确定谁不接。
Intellij: 2017.3.4
Springboot: 1.5.9
Gradle: 4.4.1
Java: 8.161
Run Code Online (Sandbox Code Playgroud) properties intellij-idea gradle spring-boot spring-boot-configuration
我维护了一个spring-boot-starter,用于定制例如在调用未知终点时返回的错误属性。这是通过覆盖org.springframework.boot.web.servlet.error.ErrorAttributes bean完成的。
在2.0.6上一切正常,但是默认情况下2.1.0禁用Bean覆盖,这使得启动器现在失败并显示以下消息。
在类路径资源[com / mycompany / springboot / starter / config / ErrorsConfig.class]中定义了名称为'errorAttributes'的无效bean定义:无法注册bean定义[root bean:class [null]; scope =; abstract = false; lazyInit = false; autowireMode = 3; dependencyCheck = 0; autowireCandidate = true; primary = false; factoryBeanName = com.mycompany.springboot.starter.config.ErrorsConfig; factoryMethodName = errorAttributes; initMethodName = null; destroyMethodName =(推断); 在类路径资源[com / mycompany / springboot / starter / config / ErrorsConfig.class]中为bean'errorAttributes'定义:已经存在[root bean:class [null]; scope =; abstract = false; lazyInit = false; autowireMode = 3; dependencyCheck = 0; autowireCandidate = true; primary …
我目前正在努力解决 SpringBootTest 实例的服务器端口注入问题。我编写了一个测试配置类,我想在其中访问此端口。
测试配置类:
@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE)
@Retention(AnnotationRetention.RUNTIME)
@Import(value = [TestTemplateConfig::class])
annotation class TestAnnotation
@Configuration
open class TestTemplateConfig {
@Value("\${server.port}")
private var localPort: Int? = null
@Bean
open fun foo() = Foo(localPort)
}
Run Code Online (Sandbox Code Playgroud)
测试看起来像这样:
@SpringBootJunit5Test
@TestAnnotation
@EnableTestCouchbase
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MyIntegrationTest {
@LocalServerPort
var port: Int = 0
@Autowired
private lateinit var foo: Foo
...
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,我总是在配置类中收到端口的零值。因为我没有得到 null,这听起来像是它正在努力获取端口,但是错误的端口(我认为零是在 spring 中为随机端口定义的)。到目前为止,MyIntegrationTest 类中服务器端口的评估工作正常。
有什么想法可以解决这个问题吗?
谢谢
是否有推荐的方法将重组/重命名引入外部化配置,同时为仍然依赖旧配置结构的消费者保持向后兼容性?
例如,给定一个库使用@ConfigurationProperties过去定义的以下配置结构:
old-properties:
an:
old-property: true
another:
custom-property: 1234
Run Code Online (Sandbox Code Playgroud)
该库的新版本将配置重新定义为如下所示:
my-library:
a-property: true
another-property: 1234
Run Code Online (Sandbox Code Playgroud)
有没有一种好方法可以弃用旧的结构,同时在一段时间内保持对现有消费者的兼容性?使用新版本库的消费者应该仍然能够使用old-properties.an.old-property并自动映射到my-library.a-property.
我知道使用附加配置元数据将属性标记为已弃用的功能,但我正在明确寻找一种支持两个版本以简化迁移的方法。
java backwards-compatibility spring-boot spring-boot-configuration
我试图在我的Spring启动应用程序中使用String Key和PromotionPolicy值将yml文件映射到HashMap,并使用默认的spring启动实现来解析值,但PromotionPolicy对象仅包含默认值[0,false,false]对于我尝试从Map中读取值的所有实例.
我的yml是:
promotionPolicies :
policies:
P001NN:
PromotionPolicy:
expiryPeriodInDays: 16
reusable: true
resetExpiry: false
P001YN:
PromotionPolicy:
expiryPeriodInDays:1
reusable:true
resetExpiry:false
P001NY:
PromotionPolicy:
expiryPeriodInDays:1
reusable:false
resetExpiry:true
Run Code Online (Sandbox Code Playgroud)
我的模型是:
public class PromotionPolicy {
private int expiryPeriodInDays;
private boolean reusable;
private boolean resetExpiry;
public int getExpiryPeriodInDays() {
return expiryPeriodInDays;
}
public void setExpiryPeriodInDays(int expiryPeriodInDays) {
this.expiryPeriodInDays = expiryPeriodInDays;
}
public boolean isReusable() {
return reusable;
}
public void setReusable(boolean reusable) {
this.reusable = reusable;
}
public boolean isResetExpiry() {
return resetExpiry;
}
public void setResetExpiry(boolean …Run Code Online (Sandbox Code Playgroud) Spring Boot 自动配置最近在 2.7 版本中进行了更改,并且大多数设置在 3.0 版本中已弃用(您可以在此处找到详细信息)。此外,他们还为自动配置类引入了新的注释,即@AutoConfiguration. 我无法理解下面所述的注释的默认设置:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore
@AutoConfigureAfter
public @interface AutoConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
为什么他们强制用户继承proxyBeanMethods = false,@AutoConfigureBefore和@AutoConfigureAfter?
spring spring-boot spring-boot-configuration spring-autoconfiguration
我有一个外部配置文件(外部 jar)。我尝试运行并期望外部文件中的值将覆盖内部文件中的值(application.properties在\resource\- jar 文件中)。我阅读文档并尝试这个:
java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties
Run Code Online (Sandbox Code Playgroud)
这不起作用。
我的 jar 文件位于\target\目录和我的production.properties太(at \target\)
我该如何解决我的问题?
spring-boot ×8
java ×4
spring ×4
docker ×2
dockerfile ×1
git ×1
gradle ×1
kotlin ×1
properties ×1
ssh-keys ×1
validation ×1
yaml ×1