springboot 配置和占位符获取配置文件中的值
程序员文章站
2022-05-03 22:08:20
...
@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
*/
@Configuration
public 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=false
persion.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=15
上一篇: SpringBoot热部署,不用重新编译
下一篇: 「从模板消息改版订阅消息」小程序推送
推荐阅读
-
获取@ContextConfiguration注解中的配置文件路径和配置文件中value
-
Springboot如何获取配置文件application.yml中自定义的变量并使用
-
spring是如何解析xml配置文件中的占位符
-
springboot在配置文件中设置特定功能的启用和停用@ConditionalOnProperty()
-
SpringBoot获取YAML配置文件中的自定义属性值
-
SpringBoot获取配置文件中的自定义属性
-
springboot之读取配置文件(application.yml)中的属性值
-
SpringBoot——实现获取配置文件里的配置值
-
springboot devtools热部署、banner.txt启动时的图像、配置文件中的随机值
-
springboot 配置和占位符获取配置文件中的值