如何使用springboot配置和占位符获取配置文件中的值
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,小编给大家分享一下如何使用springboot配置和占位符获取配置文件中的值,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!springboot配置和占位符获取配置文件值@Proper
千家信息网最后更新 2025年11月08日如何使用springboot配置和占位符获取配置文件中的值
小编给大家分享一下如何使用springboot配置和占位符获取配置文件中的值,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
springboot配置和占位符获取配置文件值
@PropertySource& 加载指定的配置文件
package com.example.springbootdemo.pojo; import com.alibaba.fastjson.JSON;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component; /** * ***GOOD LUCK**** * * @Author : Wukn * @Date : 2018/6/ * * 将配置文件中的的每一个属性的值,映射到这个组建中 *@ConfigurationProperties * prefix = "persion" 指定在配置文件中需要将persion的配置属性映射到这个实体类中 */ /** * 获取指定配置文件 * @PropertySource( value = {"classpath:coms.properties"}) */ @Component/** * @ConfigurationProperties(prefix = "persion"),默认获取根目录下的值 */@ConfigurationProperties(prefix = "persion")public class Persion { private String name; private Integer id; private Boolean bool; public Persion() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Boolean getBool() { return bool; } public void setBool(Boolean bool) { this.bool = bool; } @Override public String toString() { return JSON.toJSONString( this ); }}@ImportResource 导入指定的配置文件
以上方式过于麻烦,springboot推荐通过全注解方式,添加组件的方式
通过注解@Configration申明一个配置类,通过注解@Bean可以使用在方法上面,申明一个组件的生成,要是放在方法上,表明这个方法的返回值放在ioc容器中
package com.example.springbootdemo.configration;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;/** * Created with IntelliJ IDEA. * Description: Cms数据源的一些设置 * Date: 2018-06-08 * Time: 5:50 PM * * @author: wukn */@Configurationpublic class DataConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility( PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }通过占位符获取值
#通过使用占位符赋值persion.name=张三${random.value}persion.bool=falsepersion.id=12${random.int} person.last‐name=张三${random.uuid}person.age=${random.int} person.birth=2017/12/15 person.boss=false person.maps.k1=v1 person.maps.k2=14 person.lists=a,b,c person.dog.name=${person.hello:hello}_dog person.dog.age=15springboot配置文件,占位符的使用
首先要给到注解
让user类可用通过配置文件进行实例化
package com.example.springdemo.entity;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;import org.springframework.validation.annotation.Validated; import java.util.List; @Component//把User加到容器中@Data/** * @ConfigurationProperties * 可以将配置文件中的每一个属性的值,映射到这个组件中 * 告诉springboot将奔雷中的所有属性和配置文件中的相关属性先绑定 * prefix = "com"绑定配置文件com层级下的属性进行一一映射 * 只有是容器才能使用所以要添加注解@Component */@ConfigurationProperties(prefix = "com")public class User { private Long id; private String name; private Integer age; private List配置文件
com.email=99@dfp.comcom.name=newDFP${com.cc:不存在给默认值}com.age=${random.int}首先就是对age取随机数然后对name获取对象的数据
运行最后一个测试类
package com.example.springdemo; import com.example.springdemo.entity.User;import com.example.springdemo.mapper.UserMapper;import com.example.springdemo.properties.Myproperties;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;import org.springframework.test.context.junit4.SpringRunner; import javax.sql.DataSource;import java.sql.SQLException;import java.util.List; @SpringBootTest@RunWith(SpringRunner.class)class SpringdemoApplicationTests { //如果测试类与启动入口类包名不一致,必须加该注解属性classes指定启动入口类,否则无法启动SpringBoot @Autowired private DataSource dataSource; @Test public void dataSource() { try { System.out.println(dataSource.getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Autowired Myproperties myproperties; @Test void test(){ System.out.println("------------------------"); System.out.println(myproperties.getMes()); } @Autowired UserMapper userMapper; @Test void testMybatisPlus(){ List users=userMapper.selectList(null); for (User user:users){ System.out.println(user); } System.out.println("查询成功!"); User aduuser=new User();// aduuser.setName("DFP");// aduuser.setAge(18);// aduuser.setEmail("DFP19053025@qq.com");// aduuser.setId(19053065L); int i=userMapper.insert(aduuser); if (i>0){ System.out.println("成功加入记录!"); }else{ System.out.println("失败加入记录!");} for (User user:users){ System.out.println(user); } } @Autowired User user; @Test public void contextlodes(){ System.out.println("测试结果输出:"+user); }} 结果
因为com.cc是不存在的就回去:后面的默认值
如果com.cc存在就会取com.cc的值
测试如下
com.email=99@dfp.comcom.name=newDFP+++${com.email:不存在给默认值}com.age=${random.int}这次的值不再是默认值了com.email是存在数据的
看完了这篇文章,相信你对"如何使用springboot配置和占位符获取配置文件中的值"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
配置
文件
属性
注解
测试
容器
数据
方式
方法
组件
成功
入口
篇文章
结果
张三
一致
只有
完了
实体
实例
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海闪愿网络技术有限公司招聘
vps服务器免费版
教育软件开发
产品经理在软件开发中的作用
网络技术部面试问题及答案
数据库分库有哪些副作用
外码 数据库
cmd命令怎么导入数据库
羚城区网络安全吗
政府教育装备采购数据库
磷酸化水平数据库
金沙网络安全和信息化委员会
系统网络安全软件
校验放在前台还是数据库中好
数据库宣传标语
汕头自主可控软件开发平均价格
数据库连接运算符步骤
模拟硬件信息软件开发
网络安全需要怎么学
x79用服务器内存
哪些服务器CPU能超频
我的世界小天服务器
数据库表有重复的只显示一条
如何爬数据库数据
繁星网络安全公司
中小学生加强教育与网络安全
服务器好还是主机好
catia服务器
软件开发公司是免税企业吗
国家电网的网络安全意识