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

猜你喜欢

转载自blog.csdn.net/weixin_41404773/article/details/82425264