技术标签: SpringBoot-2.X Java spring boot java 读取配置文件的4种方式 SprinBoot-2.X 学习笔记 后端 SpringBoot配置文件
配置文件
server:
port: 8080
servlet:
context-path: /elasticsearch
spring:
application:
name: elasticsearch
在类中使用@Value注解来注入配置值
package com.xu.test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootTests {
@Value("${spring.application.name}")
private String name;
@Test
public void test() {
System.out.println(name);
}
}
elasticsearch
创建一个Java Bean类,并使用@ConfigurationProperties注解指定配置文件的前缀,然后Spring Boot会自动将配置值注入到该Bean中。
package com.xu.test.conf;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author hyacinth
*/
@Data
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class MyConf {
private String host;
private String port;
private String password;
private String timeout;
}
package com.xu.test;
import cn.hutool.json.JSONUtil;
import com.xu.test.conf.MyConf;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootTests {
@Autowired
private MyConf conf;
@Test
public void test() {
System.out.println(JSONUtil.toJsonPrettyStr(conf));
}
}
{
"host": "127.0.0.1",
"port": "6379",
"password": null,
"timeout": "10000"
}
package com.xu.test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
@SpringBootTest
class SpringBootTests {
@Autowired
private Environment env;
@Test
public void test() {
System.out.println(env.getProperty("spring.application.name"));
}
}
elasticsearch
如果有其他的配置文件,需要在配置类上使用@PropertySource注解指定配置文件的路径,然后通过@Value注解或Environment接口来读取配置值(@PropertySource只支持properties文件)。
test:
name: "测试配置文件"
package com.xu.test.conf;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.core.io.support.ResourcePropertySource;
import java.io.IOException;
import java.util.Properties;
/**
* @author hyacinth
*/
public class YamlConfigFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
name = (name != null) ? name : resource.getResource().getFilename();
if (StrUtil.isBlank(name)) {
throw new RuntimeException("配置文件不存在!");
}
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(name, new Properties());
} else if (StrUtil.containsAnyIgnoreCase(name, ".yml", ".yaml")) {
Properties yml = loadYml(resource);
return new PropertiesPropertySource(name, yml);
} else {
return new ResourcePropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
package com.xu.test.conf;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;
/**
* @author hyacinth
*/
@Data
@Component
@PropertySource(value = "classpath:new.yml", factory = YamlConfigFactory.class)
public class MyConf {
@Value("${test.name}")
private String name;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
package com.xu.test;
import cn.hutool.json.JSONUtil;
import com.xu.test.conf.MyConf;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootTests {
@Autowired
private MyConf conf;
@Test
public void test() {
System.out.println(JSONUtil.toJsonPrettyStr(conf));
}
}
{
"name": "测试配置文件"
}
server:
port: 8080
servlet:
context-path: /elasticsearch
spring:
application:
name: elasticsearch
phone: 123123123,
3123123,
312312312,
35345345,
package com.xu.test;
import cn.hutool.core.collection.CollUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringBootTests {
@Value("#{'${phone}'.replaceAll(' ', '').split(',')}")
private List<String> phone;
@Test
public void test1() {
System.out.println(phone);
}
}
文章浏览阅读119次。该楼层疑似违规已被系统折叠隐藏此楼查看此楼/***Getaparametervalue**@paramkeyString*@paramdefString*@returnString*/publicStringgetParameter(Stringkey,Stringdef){returnisStandalone?System.getProperty(ke..._java http隧道
文章浏览阅读913次。IP主机名备注192.168.117.14keepalived-master主节点192.168.117.15keepalived-slaver备节点192.168.117.100VIP1.主备节点均安装keepalived# yum install -y keepalived httpd2.主备节点均修改keepalived日志存放路径..._keepalived sendmail
文章浏览阅读469次。--==========================================--SPFILE错误导致数据库无法启动(ORA-01565)--========================================== SPFILE错误导致数据库无法启动 SQL> startup ORA-01078: failurein proce_ora01565 ora27046
文章浏览阅读6.1k次,点赞2次,收藏54次。功能测试基础知识总结_功能测试
文章浏览阅读3.2k次,点赞3次,收藏2次。pg 中文首字母排序_pg中文排序
文章浏览阅读3.1w次,点赞23次,收藏109次。本文主要讲解CONVERT函数_mysql convert
文章浏览阅读8.6k次,点赞2次,收藏2次。HTML5 的视频播放事件想必大家已经期待很久了吧,在HTML4.1、4.0之前我们如果在网页上播放视频无外乎两种方法: 第一种:安装FLASH插件或者微软发布的插件 第二种:在本地安装播放器,在线播放组件之类的 因为并不是所有的浏览器都安装了FLASH插件,就算安装也不一定所有的都能安装成功。像苹果系统就是默认禁用FLASH的,安卓虽然一开始的时候支持FLASH,但是在安卓4.0以后也开始不_微信开发者工具视频快进
文章浏览阅读5.4k次,点赞3次,收藏4次。在使用redis的过程常见错误总结1.JedisConnectionException Connection Reset参考这边文章:Connection reset原因分析和解决方案https://blog.csdn.net/cwclw/article/details/527971311.1问题描述Exception in thread "main" redis.clients...._jedisconnectionexception: java.net.socketexception: connection reset
文章浏览阅读8.3k次,点赞8次,收藏42次。目录1.Lua垃圾回收算法原理简述2.Lua垃圾回收中的三种颜色3.Lua垃圾回收详细过程4.步骤源码详解4.1新建对象阶段4.2触发条件4.3 GC函数状态机4.4标记阶段4.5清除阶段5.总结参考资料lua垃圾回收(Garbage Collect)是lua中一个比较重要的部分。由于lua源码版本变迁,目前大多数有关这个方面的文章都还是基于lua5.1版本,有一定的滞后性。因此本文通过参考当前..._lua5.3 gc
文章浏览阅读511次。最近家中的潮人,老妈闲着没事干,开始学玩电脑,引起他的各种好奇心。如看看新闻,上上微信或做做其他的事情。但意料之中的是电脑上会莫名出现各种问题?不翼而飞的图标?照片又不见了?文件被删了,卡机或者黑屏,无声音了,等等问题。常常让她束手无策,求助于我,可惜在电话中说不清,往往只能苦等我回家后才能解决,那种开心乐趣一下子消失了。想想,这样也不是办法啊, 于是,我潜心寻找了两款优秀的远程控制软件。两款软件...
文章浏览阅读1.8k次。二.初始化工作空间三.设置下载地址四.下载功能包此处可能会报错,请看:rosdep update遇到ERROR: error loading sources list: The read operation timed out问题_DD᭄ꦿng的博客-程序员宅基地接下来一次安装所有功能包,注意对应ROS版本 五.编译功能包isolated:单独编译各个功能包,每个功能包之间不产生依赖。编译过程时间比较长,可能需要几分钟时间。此处可能会报错:缺少absl依赖包_ros18.04 安装ca
文章浏览阅读4.1k次,点赞3次,收藏7次。Haobor2.2.1配置(trivy扫描器、镜像签名)docker-compose下载https://github.com/docker/compose/releases安装cp docker-compose /usr/local/binchmod +x /usr/local/bin/docker-composeharbor下载https://github.com/goharbor/harbor/releases解压tar xf xxx.tgx配置harbor根下建立:mkd_init error: db error: failed to download vulnerability db: database download