springboot项目架构(2)--- 配置外部配置文件

2018-06-19  本文已影响229人  贺宇_f482

在原来做普通web项目的时候,我会用

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

来把spring的配置文件放到系统环境变量下。但是在最初转到springboot的时候,我并没有找到把配置文件放到外部的方法,后来过了一段时间终于找到了外置配置文件的一些方法,我这里只介绍我在用的一种方法。

1、springboot的配置文件外置:

@SpringBootApplication
@MapperScan(basePackages = "com.pcbwx.jsp.dao") // mybatis包路径
public class SystemStart extends SpringBootServletInitializer {
    // springBoot配置文件名字
    private static final String FILENAME = "spring.properties";
    // 系统英文简写
    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的配置文件外置就是在项目启动的时候修改一下默认的配置文件,比较简单,但是有一点要注意,就是在环境变量的配置文件中使用 spring.profiles.active=config 属性的时候,额外的配置文件不会再和这个配置文件在一个文件夹下,而是会在项目的classpath下。

2、定时任务配置

@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就不用说了,直接写在配置文件中就可以了,但是如果你想用fixedRate(fixedDelay)的话,你就必须把fixedRate或者fixedDelay换为fixedRateString(fixedDelayString),配置项的结果必须换成数值类型,乘法表达式是不可以的。

#定时调度,以下均为一分钟
reload.timer.fixedRate = 60000
plan.generate.timer.corn = 0 0/1 * ? * *

3、日志文件配置

这个只需在springboot的配置文件中指出就好了,就是上方1条目中。

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

4、普通配置文件

其实普通变量也可以写在springboot配置文件中,然后用@Value("${xxx}")来获取,但是有时候我们会在静态方法中用到这些变量,而静态变量是不能用@Value注解来赋值的,所以我还是用了原来普通web项目的方法,同样是放在环境变量中:

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的这个类,只不过在这里它是静态的。我们用Properties.getProperty方法就可以获取到配置文件中的信息。

项目地址:https://github.com/HeyuRise/jsp

上一篇 下一篇

猜你喜欢

热点阅读