技术标签: 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);
}
}
文章浏览阅读634次。对于copy功能PostgreSQL从9.2.4到16devel是否有过优化?或者openGauss是否持续合并或优化PostgreSQL的copy功能,这方面我没有去考证过。单纯从测试结果上看,openGauss的copy性能要略逊于PostgreSQL。当然,可能是我水平有限,所以希望各位openGauss的专家、老师集思广益,还openGauss一个真实的COPY FROM文件导入性能。(大家可以回复优化方案,我这边去做验证)_copy to or from a file is prohibited for security concerns
文章浏览阅读1.1k次,点赞23次,收藏27次。基于springboot的体育馆使用预约系统_基于springboot的体育馆预约管理系统
文章浏览阅读390次。SpringSpring Bean 的作用域有哪些?它的注册方式有几种?Spring 容器中管理一个或多个 Bean,这些 Bean 的定义表示为 BeanDefinition 对象,具体包含以下重要信息:Bean 的实际实现类;Bean 的引用或者依赖项;Bean 的作用范围;singleton:单例(默认);prototype:原型,每次调用bean都会创建新实例;request:每次http请求都会创建新的bean;session:同一个http session共享一个bean_spring和springboot的常见面试题
文章浏览阅读1.9k次。Openstack认证服务(认证组件)3_openstack 认证服务
文章浏览阅读4.5k次。第11节:人与人的相处(1) 2006-6-7 8∶40∶00 人与人的相处 一、有后台的下属。 我遇见过,也处理得很好。你不能得罪他背后的人,那么就通过他去利用他背后的人。比如说他是老板的亲戚,碰见别的部门有什么搞不定的人,你美言他几句叫他去搞,成功了自然是别人给老板面子,失败了你也可以多多积累他的错误,日后真到不得不踢人的时候也派得上..._外企重视documentation
文章浏览阅读3.4k次。这个应用程序是用iOS 15.5 SDK构建的。从2023年4月开始,所有提交到应用商店的iOS应用程序都必须使用iOS 16.1 SDK或更高版本构建,包括在Xcode 14.1或更高版本中。目前iOS 开发工具Xcode 版本号是13.4.1 ,系统无法升级,也会导致Xcode无法升级。1、苹果官方提示: 2023年4月开始,开发必须使用 Xcode 14.1 以上的版本,2、目前此电脑无法在升级, 2023年4月开始 ,此电脑就无法正常开发使用,应用程序商店连接操作错误。_sdk version issue. this app was built with the ios 15.5 sdk. all ios and ipa
文章浏览阅读1.8w次,点赞2次,收藏65次。前端如何使用以太坊智能合约方法这里讲的是前端与MetaMask之间的交互文中涉及到的官方文档web3.js 1.0中文手册MetaMask官方文档web3.js文件链接:https://pan.baidu.com/s/1_mPT-ZcQ9GU_U1CVhBKpLA提取码:cbey//在vue中安装web3npm install web3 --save//在main.js引入import Web3 from 'web3'Vue.prototype.Web3 = Web3一、唤起Me_如何使用web3和vue.js创建你的第一个以太坊dapp
文章浏览阅读701次。绘制一个太阳花的图形。from turtle import *color("red",'yellow')begin_fill()while True: forward(200) left(170) if abs(pos())<1: breakend_fill()done()_绘制一个直径随机的太阳花
文章浏览阅读348次。Linux常用命令(1)切换到ROOT用户(su - root)[liu@localhost ~]$ su - root密码:[root@localhost ~]查看IP地址(ifconfig)[root@localhost ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.47 netmask 255.255.255.0 broad_code=exited, status=0/success
文章浏览阅读1.3k次。直接上代码:fanweiss(){//画圈varaaa=this.gaojingDatadebuggervarmap=newBMap.Map("ydmap");//创建Map实例varmPoint=newBMap.Point(this.gaojingData.longitude,this.gaojingData.latitude);//中心点map.setMapStyle({style:"midni..._bmap.circle
文章浏览阅读1.4k次。VisualVM原插件地址是oracle的打不开,已经移到github上了,具体如下:介绍:https://visualvm.github.io/plugins.html下载地址:https://visualvm.github.io/pluginscenters.html 选择对应JDK版本下载即可! 注意事项:在使用Visual VM进行heapdump分析的时候,发..._visualvm 插件中心地址
文章浏览阅读8.8k次,点赞15次,收藏80次。understand 常用操作文章目录understand 常用操作简单介绍软件下载常用基本操作新建工程并添加现有文件如何找到自己当前想要去编辑的文件?如何在当前文件中找到你要编辑的函数?如何跳转到定义?查看当前文件的函数列表如何查看函数都被谁调用了?查看函数的调用逻辑如何查找如何找到函数的被调用图除此之外可以分析出代码的各种结构文本的编辑格式设置双屏一边看代码,一遍看代码地图简单介绍understand对分析代码有非常强的能力,完全可以代替sourceinsight,并且可以在linux上mac上使_understand代码