在原來(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