关键词:
springboot项目中,当我们使用@Value注解读取配置属性,默认的配置文件是properties类型文件,如果一些配置来自yaml格式配置文件,那么就需要做一个配置。PropertySource注解提供了factory属性,可以设置yaml格式文件加载工厂类。
下面介绍如何自定义factory实现yaml配置文件加载。
项目准备:
maven工程pom.xml增加spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
自定义YamlPropertySourceFactory
package com.xxx.web.config;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.List;
public class YamlPropertySourceFactory implements PropertySourceFactory
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException
List<PropertySource<?>> list = new YamlPropertySourceLoader().load(resource.getResource().getFilename(),resource.getResource());
if(list != null && list.size() > 0)
return list.get(0);
return null;
yml配置文件使用
package com.xxx.web.controller;
import com.xxx.web.config.YamlPropertySourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/test/v1")
@PropertySource(value = "file:conf/test.yml",factory = YamlPropertySourceFactory.class)
public class TestController
@Value("$business.errorCode")
private int errorCode;
@Value("$business.msg")
private String msg;
@Value("$business.data")
private String data;
@RequestMapping(value = "/status",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity queryStatus()
Map<String,Object> result = new HashMap()
put("msg",msg);
put("errorCode",errorCode);
put("data",data);
;
return ResponseEntity.ok(result);
这里为了配合项目配置,配置文件放在工程根目录下的conf/test.yml
business:
errorCode: 2
msg: success,
data: hallo
启动项目,我们可以访问地址http://localhost:8080/test/v1/status,可以看到,返回结果为配置文件中设置的值。
这里值得注意的是,@PropertySource在指定配置文件位置的时候,使用的是file,这种方式指定的路径,是相对于项目根路径来的,所以我们的配置文件没有放在resources目录下,如果放在resources目录下,要通过file访问到,那么,位置需要设置成这样:
@PropertySource(value="file:src/main/resources/conf/test.yml",factory=YamlPropertySourceFactory.class)
这里还可以通过classpath的方式来指定,如果conf/test.yml放在了resources类路径下。就可以这样设置:
@PropertySource(value = "classpath:conf/test.yml",factory = YamlPropertySourceFactory.class)
注意两种方式的区别,以及配置文件需要放置的位置。
springboot从入门到精通-项目搭建
参考技术ASpringBoot极大的简化了java项目的开发,在之前如果想要开发一个java项目,需要安装tomcat或者其他容器插件。但是SpringBoot内部已经集成了tomcat,因此项目的启动异常的方便。而且SpringBoot的开发中有很多默认的配置,帮助... 查看详情
idea中基于mongodb,创建springboot项目
idea中基于MongoDB,创建SpringBoot项目,详细截图如下: 查看详情
springboot.11.idea中如何快速复制当前父项目中的一个module为新的项目(代码片段)
SpringBoot.11.IDEA中如何快速复制当前父项目中的一个Module为新的项目实现步骤1.复制项目2.引入项目3.配置项目4.测试IDEA中如何快速复制当前父项目中的一个Module为新的项目呢?这个做法其实跟我们之前的文章Springboot.10.IDEA中如... 查看详情
springboot.11.idea中如何快速复制当前父项目中的一个module为新的项目(代码片段)
SpringBoot.11.IDEA中如何快速复制当前父项目中的一个Module为新的项目实现步骤1.复制项目2.引入项目3.配置项目4.测试IDEA中如何快速复制当前父项目中的一个Module为新的项目呢?这个做法其实跟我们之前的文章Springboot.10.IDEA中如... 查看详情
docker运行jenkins及vue项目与springboot项目(五.jenkins打包springboot服务且在docker中运行)
docker运行jenkins及vue项目与springboot项目:一.安装docker二.docker运行jenkins为自动打包运行做准备三.jenkins的使用及自动打包vue项目四.docker运行nginx五.jenkins打包springboot服务且在docker中运行1.立即构建输入任务名称:service_blog选择maven项... 查看详情
idea在springboot项目中显示rundashboard
1.Run->EditConfigurations2.在Templates【Defaults】->ConfigurationsavailableinRunDashboard中添加需要显示Dashboard的项目。 查看详情
springboot.10.idea中如何快速复制一个maven构建的springboot项目
如何在IDEA中复制一个Maven构建的springboot项目,我们使用nginx虚拟主机使用的项目来演示一下这个步骤首先我们使用hf58这个项目,进入到workspace目录,找到这个项目复制一份将文件夹改名字为ah58,改完这个名字后... 查看详情
springboot项目中,aop的使用
Springboot中自带依赖1.创建一个SellerAuthorizeAspect类,打上标签@Aspect和@Component@Aspect@Component@Slf4jpublicclassSellerAuthorizeAspect{}2.设置切点,这个注解的意思是拦截所有controller中Seller*开头的类的方法但是不拦截SellerUserController中的方法@Poi 查看详情
在 Camel SpringBoot 项目中传播请求参数值
】在CamelSpringBoot项目中传播请求参数值【英文标题】:PropagaterequestparamvalueinCamelSpringBootproject【发布时间】:2019-08-1510:22:14【问题描述】:您好,我有SpringBoot2项目,我正在使用骆驼作为路线。我有一个骆驼休息端点和骆驼路线... 查看详情
springboot项目创建文件中相对路径问题
Springboot项目创建文件中相对路径问题原代码:Stringlocation="./src/main/resources/UsersFiles/"+userId+"/";Filefile=newFile(location);上面代码在没有tomcat环境下可以正常使用。当如果在tomcat之下。就会在所需的路径之前加上一个用... 查看详情
怎么看出来项目中是不是用了springboot项目
参考技术ASpringBoot项目特点很多:比如说,依赖:springboot大多是套餐类的起步依赖,spring-boot-starter格式的;再比如,配置文件:springboot的配置文件格式不是xml,而是properties或者yml或者是yaml;还有,启动方式:springboot项目会自... 查看详情
基于springboot项目的https
...很简单。在application.properties中server.port:8080这样配置后,springboot内嵌的tomcat服务器就是跑在8080端口启动http服务。但是如果在配置中启动https服务,用到的端口也是server.port。spring不支持同时在配置中启动http和https。但是如果这样... 查看详情
springboot项目指定访问路径有项目名
springboot在tomcat中访问需要加项目名,在一般启动直接地址加端口就可以访问了。在一般项目中如果要加项目名访问,只需在配置文件中设置就行了server.context-path=/项目名 查看详情
从 SpringBoot 项目的属性文件中获取 Maven basedir
】从SpringBoot项目的属性文件中获取Mavenbasedir【英文标题】:GetMavenbasedirfromapropertiesfileinaSpringBootproject【发布时间】:2019-10-0400:37:47【问题描述】:我继承的Maven项目有一些JUnits中使用的资源文件。这些文件在属性文件中被称为绝... 查看详情
idea中springboot项目单元测试
参考技术A开发中我们经常会对一些方法类做单元测试,我们普遍习惯使用main方法来测试,而忽略了单元测试。右键测试类,选择runwithCoverage可查到单元测覆盖率 查看详情
docker中使用dockerfile部署springboot项目
1、准备springboot项目jar包和Dockerfile文件FROMjava:8VOLUME/tmpADDeladmin.jareladmin.jarEXPOSE8080ENTRYPOINT["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eladmin.jar"]FROM:表示基础镜像,即运行环境VOLUME/tmp创建/ 查看详情
在带有 MVC REST API 的 SpringBoot 项目中返回 406 响应
】在带有MVCRESTAPI的SpringBoot项目中返回406响应【英文标题】:InaSpringBootprojectwithMVCRESTAPIisreturning406response【发布时间】:2021-12-0112:04:03【问题描述】:我正在开发一个SpringBoot项目,该项目使用常规MVC机制来公开RESTAPI。在一个特定... 查看详情
window中使用bat启动springboot项目,并解决乱码问题
目录1.springboot项目打包jar2.编写bat启动springboot脚本3.bat启动springboot4.常见问题4.1.解决bat控制台中文乱码问题4.2.cd%~dp0不是内部或外部命令1.springboot项目打包jar打包后,在如下目录:这里打包的springboot的jar文件名为 api-0.0.1... 查看详情