项目加载时,配置文件读取参数并把参数放在缓存中。


import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;


import com.zenith.shuttle.jaxws.app.Settings;


import javax.annotation.PostConstruct;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Lazy(false)
@Service 
public class SmsSettings {
    
    
    public static final String CORG_CODE = "sms";
   
    private String confRootPath;


    private Properties properties;


    private String corgName;


    private String confCorgPath;


    private String configFileName;
   
    public SmsSettings() throws IOException {
        this.corgName = CORG_CODE;
        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);
                    }
                }
            }
        }
    }

}

需要的jar包 maven 配置

<dependency>
            <groupId>com.zenith.shuttle.components</groupId>
            <artifactId>jaxws-ec</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值