关键词:
【中文标题】无法从 Spring Boot 应用程序中的自定义 yml 文件加载配置【英文标题】:Cannot load config from custom yml file in spring boot application 【发布时间】:2017-11-16 08:30:55 【问题描述】:我正在从我的 Spring Boot 服务中的 application.yml 加载自定义配置。
我已经通过bean类注释如下,
@Component
@ConfigurationProperties("app")
public class ContinentConfig
private Map<String, List<Country>> continents = new HashMap<String, List<Country>>();
//get/set/tostring methods
我的自定义类 Country 包括 2 个字段,
public class Country
String name;
String capital;
//get/set/tostring methods
在 application.yml 我有如下,
app:
continents:
Europe:
- name: France
capital: Paris
Asia:
- name: China
capital: Beijing
通过上述设置,我可以从 application.yml 加载配置。
我现在想将配置提取到同一 src/main/resources 文件夹中的单独 continentconfig.yml 中。因此,我将自定义配置移到了continentconfig.yml,而将server.port 等其他属性留在了application.yml 中。
continentconfig.yml 的内容与我之前在 application.yml 中的内容相同。
我还在 ContinentConfig 类中添加了以下注释,
@Component
@ConfigurationProperties("app")
@EnableConfigurationProperties
@PropertySource(value="classpath:continentconfig.yml")
public class ContinentConfig
在此更改之后,我看到配置没有从 continentconfig.yml 加载到 ContinentConfig bean。
有人可以帮忙解决这个问题吗?
【问题讨论】:
在这里看看这个答案:https://***.com/questions/21271468/spring-propertysource-using-yaml/54247009#54247009 .它很容易使用。 【参考方案1】:简短的回答你不能这样做,你应该使用属性文件。
24.6.4 YAML 缺点
YAML 文件无法通过 @PropertySource 注解加载。所以在 如果您需要以这种方式加载值,则需要使用 属性文件。
您可以创建初始化程序并使用YamlPropertySourceLoader
。
【讨论】:
感谢您的澄清。但是,是否有另一种方法可以在它自己的 yml 文件中外部化自定义配置,也许将其用作特定于配置文件的 yml 文件。 您可以在application.yml
中拥有一个基本配置,并且您可以为所需的配置文件application-dev.yml
等覆盖它。使用YamlPropertySourceLoader
还有另一种解决方法。见***.com/questions/21271468/…【参考方案2】:
我相信外部化是指使用属性文件从 github/或其他此类托管存储库中配置的文件加载配置?您可以使用 bootstrap.yml 很好地做到这一点。这会从外部文件加载所有配置,并允许配置使用本地 application.properties/application.yml 覆盖它们
弹簧: 应用: 姓名: 云 : 配置: 网址:
还要确保你的 pom 中有 spring cloud 来解决这个问题,以防万一
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
如果您的本地 yml 属性文件未加载到类路径中,请添加以下内容
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.jks</include>
</includes>
</resource>
</resources>
注意:最好让 YamlPropertySourceLoader 将您的配置文件加载到您的类路径,除此之外,您还可以使用上述配置
【讨论】:
Spring Boot 中的自定义异常
】SpringBoot中的自定义异常【英文标题】:CustomExceptioninSprinBoot【发布时间】:2019-12-1209:54:06【问题描述】:我在SPRINGBOOT中编写了以下自定义错误处理程序@RestControllerAdvicepublicclassCustomGlobalExceptionHandlerextendsResponseEntityExceptionHandler@... 查看详情
带有spring-boot的自定义sql中的Liquibase参数
...2019-04-1910:16:51【问题描述】:我在jdk11上有一个spring-boot应用程序,使用maven,具有以下liquibase依赖项:<dependency><groupId>org.liquibase< 查看详情
从 grails 应用程序中的自定义 groovy 文件加载 spring bean
】从grails应用程序中的自定义groovy文件加载springbean【英文标题】:Loadspringbeansfromcustomgroovyfilesingrailsapp【发布时间】:2014-07-2413:41:39【问题描述】:尝试从Grails2.3.7.中的自定义groovy文件加载springbean我知道这个问题之前已经被问... 查看详情
Spring Boot JPA中的自定义查询问题
】SpringBootJPA中的自定义查询问题【英文标题】:ProblemwithcustomQueryinSpringbootJPA【发布时间】:2020-07-0502:06:49【问题描述】:我正在尝试按范围计算记录数,但在运行服务时出现错误:Causedby:org.hibernate.hql.internal.ast.QuerySyntaxException:... 查看详情
用于指定分发管理的自定义 spring boot 启动器
...间】:2017-01-0801:23:22【问题描述】:我们有一个SpringBoot应用程序,它的父级定义为spring-boot-starter-parent。但是在我们的项目中,我们有一个定义了分发管理的父pom,并且项目中的所有子模块都继承自 查看详情
尝试在spring boot中将env变量读取到不在application.property中的自定义属性文件
】尝试在springboot中将env变量读取到不在application.property中的自定义属性文件【英文标题】:tryingtoreadenvvariablestocustompropertiesfilenotinapplication.propertyinspringboot【发布时间】:2021-01-0510:45:27【问题描述】:我正在尝试从custom.properties... 查看详情
Spring Boot - 实体中的自定义类字段
】SpringBoot-实体中的自定义类字段【英文标题】:SpringBoot-customclassfieldinentity【发布时间】:2017-11-2620:15:20【问题描述】:我有2个自定义类,OzBakim和GunlukEtkinlik。这些类不是实体。我需要在实体中使用这些类。但我得到一个错误... 查看详情
Spring boot - Application.properties 中的自定义变量
】Springboot-Application.properties中的自定义变量【英文标题】:Springboot-customvariablesinApplication.properties【发布时间】:2015-11-1014:05:59【问题描述】:我有一个使用restfulapi的springboot客户端。我可以使用application.properties中的任何密钥条... 查看详情
无法在 Spring Boot Security 中登录到我的自定义登录页面
】无法在SpringBootSecurity中登录到我的自定义登录页面【英文标题】:Can\'tlogintomycustomloginpageinspringbootsecurity【发布时间】:2015-04-2509:29:19【问题描述】:我的问题是:当我尝试在我的自定义登录页面上登录时,它会再次将我重定... 查看详情
java - 如何在spring boot java中编写一个函数来处理JPA存储库中的自定义查询?
】java-如何在springbootjava中编写一个函数来处理JPA存储库中的自定义查询?【英文标题】:HowtowriteafunctiontohandlecustomqueryinJPArepositoryinspringbootjava?【发布时间】:2019-09-0312:14:14【问题描述】:基本上,我使用SpringBoot在服务器端托管... 查看详情
外部tomcat中的自定义上下文路径
...题描述】:我制作了一个想在外部tomcat8中运行的Spring-Boot应用程序。在Spring-Boot应用程序中,可以使用application.properties中的属性server.context-path选择上下文路径,但由于我使用的是外部tomcat8 查看详情
Spring Boot,MongoDB,Pageable,按对象中的自定义方法排序
】SpringBoot,MongoDB,Pageable,按对象中的自定义方法排序【英文标题】:SpringBoot,MongoDB,Pageable,sortbyacustommethodintheobject【发布时间】:2020-04-2214:27:46【问题描述】:比如说我有以下设置,这样的模型:publicclassPost@IdprivateStringid;privat... 查看详情
无法从 Java Spring Boot 项目中的 application.yml 文件中读取用户定义类的列表
】无法从JavaSpringBoot项目中的application.yml文件中读取用户定义类的列表【英文标题】:Unabletoreadlistofuserdefinedclassfromapplication.ymlfileinaJavaSpringBootproject【发布时间】:2021-02-1302:45:43【问题描述】:团队您好,我最近尝试从SpringBoot项... 查看详情
无法从 JUnit 中的 Spring Boot 读取应用程序属性?
】无法从JUnit中的SpringBoot读取应用程序属性?【英文标题】:NotabletoreadapplicationpropertiesfromSpringBootinJUnit?【发布时间】:2017-06-2613:16:32【问题描述】:我有一个JUnit测试,我想针对REST服务运行该服务,该服务已经在我的机器上的... 查看详情
如何允许 Spring Boot 应用程序使用具有 Spring Cloud 依赖关系的自定义 jar
】如何允许SpringBoot应用程序使用具有SpringCloud依赖关系的自定义jar【英文标题】:Howtoallowspringbootapplicationstousecustomjarhavingspringclouddependency【发布时间】:2021-06-2520:09:30【问题描述】:我有许多SpringBoot微服务,并且我开发了一个... 查看详情
使用@sql spring boot test从资源文件夹以外的自定义位置读取.sql文件
】使用@sqlspringboottest从资源文件夹以外的自定义位置读取.sql文件【英文标题】:Reading.sqlfilefromcustomlocationotherthanresoucesfolderusing@sqlspringboottest【发布时间】:2020-11-1400:21:13【问题描述】:您好,我已经使用springdatajpatest编写了测... 查看详情
spring boot 自定义登录页面
...】:2016-10-3112:01:44【问题描述】:我正在尝试为我的引导应用程序添加一个自定义登录页面。我正在关注this教程。我无法使用我的自定义登录页面。这是我的pom.xml:...<dependency><groupId>org.springframework.data</groupId& 查看详情
无法从 Spring Boot 应用程序运行 flyway 脚本,数据库为 docker 容器中的 Mysql
】无法从SpringBoot应用程序运行flyway脚本,数据库为docker容器中的Mysql【英文标题】:UnabletoruntheflywayscriptsfromSpringBootapplicationwithdatabaseasMysqlindockercontainer【发布时间】:2018-11-1014:55:30【问题描述】:我有一个springboot应用程序,它... 查看详情