配置文件的读取



@Lazy(false)
@Service
public class BcmSettings extends BaseSettings {


    /**
     * 合作机构编码
     */
    public static final String CORG_CODE = "bcm";


    public BcmSettings() throws IOException {
        super(CORG_CODE);
    }


}


public class BaseSettings {
    private static Logger logger =Logger.getLogger(BaseSettings.class);
    
    
    private String confRootPath;


    private Properties properties;


    private String corgName;


    private String confCorgPath;


    private String configFileName;




    public BaseSettings(String corgName) throws IOException {
        this.corgName = corgName;
        File rootFile = Settings.getPrefFolder();
        if(rootFile==null){
            Settings.setPrefFolder("src"+File.separator+"test"+File.separator+"resources"+File.separator);
        }
        confRootPath = Settings.getPrefFolder().getAbsolutePath()+File.separator;
        confCorgPath = confRootPath+corgName+File.separator;
        configFileName = corgName+".properties";


    }
    


    public String getConfRootPath(){
        return confRootPath;
    }


    public Object get(String key) {
        return properties.get(key);
    }


    public String getString(String key) {
        return (String) properties.get(key);
    }


    @PostConstruct
    public void init () {


        File settingsFile = new File(confCorgPath+configFileName);
        if(settingsFile.exists()){
            properties = new Properties();
            InputStream inputStream = null;
            try{
                inputStream = new FileInputStream(settingsFile);
                properties.load(inputStream);
            }catch (Exception e){
                logger.error("主框架配置加载初始化异常",e);
            }finally {
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    }catch (Exception e){
                        logger.error("主框架配置加载流关闭异常",e);
                    }
                }
            }
        }
    }
}




调用

@Autowired
BcmSettings bcmSettings;

bcmSettings.getString("配置文件的key名")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值