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
 */
@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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

托尼吴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值