springboot項(xiàng)目架構(gòu)(2)--- 配置外部配置文件

在原來(lái)做普通web項(xiàng)目的時(shí)候,我會(huì)用

location="file:${CONFIG_SPACE}/jsp/spring.properties" 

來(lái)把spring的配置文件放到系統(tǒng)環(huán)境變量下。但是在最初轉(zhuǎn)到springboot的時(shí)候,我并沒(méi)有找到把配置文件放到外部的方法,后來(lái)過(guò)了一段時(shí)間終于找到了外置配置文件的一些方法,我這里只介紹我在用的一種方法。

1、springboot的配置文件外置:

@SpringBootApplication
@MapperScan(basePackages = "com.pcbwx.jsp.dao") // mybatis包路徑
public class SystemStart extends SpringBootServletInitializer {
    // springBoot配置文件名字
    private static final String FILENAME = "spring.properties";
    // 系統(tǒng)英文簡(jiǎn)寫
    public final static String MYSYSTEMCODE = "jsp";

    public static void main(String[] args) throws Exception {
        // 指定配置文件
        Properties properties = new Properties();
        String configFile = System.getenv("CONFIG_SPACE") + "/" + MYSYSTEMCODE + "/" + FILENAME;
        InputStream in = new FileInputStream(new File(configFile));
        properties.load(in);
        SpringApplication springApplication = new SpringApplication(SystemStart.class);
        springApplication.setDefaultProperties(properties);
        springApplication.run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SystemStart.class);
    }
}

springboot的配置文件外置就是在項(xiàng)目啟動(dòng)的時(shí)候修改一下默認(rèn)的配置文件,比較簡(jiǎn)單,但是有一點(diǎn)要注意,就是在環(huán)境變量的配置文件中使用 spring.profiles.active=config 屬性的時(shí)候,額外的配置文件不會(huì)再和這個(gè)配置文件在一個(gè)文件夾下,而是會(huì)在項(xiàng)目的classpath下。

2、定時(shí)任務(wù)配置

@Configuration
@EnableScheduling
public class QuartzJob {

    @Autowired
    private SupportService supportService;

    private static AtomicInteger reloadFlag = new AtomicInteger();
    @Scheduled(fixedRateString = "${reload.timer.fixedRate}") 
    public void reloadCache() {
        if (reloadFlag.incrementAndGet() > 1) {
            reloadFlag.decrementAndGet();
            return;
        }
        // TODO
        reloadFlag.decrementAndGet();       
    }

    private static AtomicInteger planGenerateFlag = new AtomicInteger();    
    @Scheduled(cron = "${plan.generate.timer.corn}")
    public void generateExePlan() {
        if (planGenerateFlag.incrementAndGet() > 1) {
            planGenerateFlag.decrementAndGet();
            return;
        }
        // TODO
        planGenerateFlag.decrementAndGet(); 
    }
}

corn就不用說(shuō)了,直接寫在配置文件中就可以了,但是如果你想用fixedRate(fixedDelay)的話,你就必須把fixedRate或者fixedDelay換為fixedRateString(fixedDelayString),配置項(xiàng)的結(jié)果必須換成數(shù)值類型,乘法表達(dá)式是不可以的。

#定時(shí)調(diào)度,以下均為一分鐘
reload.timer.fixedRate = 60000
plan.generate.timer.corn = 0 0/1 * ? * *

3、日志文件配置

這個(gè)只需在springboot的配置文件中指出就好了,就是上方1條目中。

#指定log4j2配置文件
logging.config=classpath:log4j2-dev.xml

4、普通配置文件

其實(shí)普通變量也可以寫在springboot配置文件中,然后用@Value("${xxx}")來(lái)獲取,但是有時(shí)候我們會(huì)在靜態(tài)方法中用到這些變量,而靜態(tài)變量是不能用@Value注解來(lái)賦值的,所以我還是用了原來(lái)普通web項(xiàng)目的方法,同樣是放在環(huán)境變量中:

public class ConfigProperties {

    private static final String FILENAME = "config.properties";

    public static final Properties props = new Properties();

    static {
        String configFile = System.getenv("CONFIG_SPACE") + "/" + SystemStart.MYSYSTEMCODE + "/" + FILENAME;
        InputStream in = null;
        try {
            in = new FileInputStream(new File(configFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (in != null) {
            try {
                props.load(in);
            } catch (IOException e) {
                throw new BusinessException(ExceptionType.EXCEPTION_400, "未找到" + FILENAME + "文件");
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }

    public static String getProperty(ConfigEnum config) {
        return props.getProperty(config.getCode());
    }

    public static String getProperty(ConfigEnum config, String defValue) {
        String value = props.getProperty(config.getCode());
        if (value == null) {
            return defValue;
        }
        return value;
    }

    public static Integer getPropertyInt(ConfigEnum constant) {
        String value = props.getProperty(constant.getCode());
        if (value == null) {
            return null;
        }
        return Integer.valueOf(value);
    }

    public static Integer getPropertyInt(ConfigEnum constant, Integer defValue) {
        String value = props.getProperty(constant.getCode());
        if (value == null) {
            return defValue;
        }
        return Integer.valueOf(value);
    }
}

我們這里一樣用了Properties的這個(gè)類,只不過(guò)在這里它是靜態(tài)的。我們用Properties.getProperty方法就可以獲取到配置文件中的信息。

項(xiàng)目地址:https://github.com/HeyuRise/jsp

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容