spring boot加载properties文件代码示例

package com.leju.robot.customer.config;

import com.leju.robot.customer.common.consts.RobotConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.annotation.PostConstruct;

/**
 * 对外接口相关配置
 * Created by Administrator on 2018/8/8.
 */

@Configuration//被spring容器管理
@PropertySource("classpath:env.properties")//加载properties的位置,如果不写默认加载的是application.properties
public class EnvConfig {

    private static final Logger logger = LoggerFactory.getLogger(EnvConfig.class);

    @Value("${zygw.query.list.url}")//使用@value注解获取properties中的文件
    private String zygwUrl;

    @Value("${contentpool.houses.url}")
    private String contentPoolHousesUrl;

    @Value("${contentpool.houses.type}")
    private String contentPoolHousesType;

    @Value("${contentpool.houses.key}")
    private String contentPoolHousesKey;

    @Value("${contentpool.houses.appid}")
    private String contentPoolHousesAppid;


    @PostConstruct//在servlet初始化的时候加载,并且只加载一次,和构造代码块的作用类似
    private void init(){
        logger.info("load env.properties start!");
        RobotConsts.API_ZYGW_URL = zygwUrl;
        RobotConsts.API_CONTENT_HOUSES_URL = contentPoolHousesUrl;
        RobotConsts.CONTENT_HOUSES_TYPE = contentPoolHousesType;
        RobotConsts.CONTENT_HOUSES_KEY = contentPoolHousesKey;
        RobotConsts.CONTENT_HOUSES_APPID = contentPoolHousesAppid;
        logger.info("load env.properties end!");
    }



}
package com.leju.robot.customer.common.consts;

/**
 * Created by Administrator on 2018/7/26.
 */
public class RobotConsts {

    /**
     * 根据城市id和楼盘id获取置业顾问列表的接口地址
     */
    public static  String API_ZYGW_URL = "";

    /**
     * 内容池-楼盘库 搜索接口地址
     */
    public static  String API_CONTENT_HOUSES_URL = "";

    /**
     * 内容池-楼盘库 查询类型
     */
    public static  String CONTENT_HOUSES_TYPE = "";

    /**
     * 内容池-楼盘库 向内容池申请的对应业务Key
     */
    public static  String CONTENT_HOUSES_KEY = "";

    /**
     * 内容池-楼盘库 向内容池申请的对应业务的APPID
     */
    public static  String CONTENT_HOUSES_APPID = "";





}

spring boot加载properties文件代码示例_第1张图片

你可能感兴趣的:(原创)