diff --git a/urule-springboot/pom.xml b/urule-springboot/pom.xml index d93edf3e..093c0d2f 100644 --- a/urule-springboot/pom.xml +++ b/urule-springboot/pom.xml @@ -53,6 +53,11 @@ + + mysql + mysql-connector-java + 5.1.45 + javax.servlet servlet-api diff --git a/urule-springboot/src/main/java/com/bstek/urule/springboot/PropertiesConfiguration.java b/urule-springboot/src/main/java/com/bstek/urule/springboot/PropertiesConfiguration.java index f1e5591f..cd1559d1 100644 --- a/urule-springboot/src/main/java/com/bstek/urule/springboot/PropertiesConfiguration.java +++ b/urule-springboot/src/main/java/com/bstek/urule/springboot/PropertiesConfiguration.java @@ -1,21 +1,59 @@ package com.bstek.urule.springboot; -import java.util.Properties; - +import com.bstek.urule.URulePropertyPlaceholderConfigurer; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; -import com.bstek.urule.URulePropertyPlaceholderConfigurer; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; /** * @author Jacky.gao * @since 2016年10月12日 + * @see com.bstek.urule.springboot.SpringBootPropertyPlaceholderAutoConfiguration */ -//@Component -public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer implements InitializingBean{ - @Override - public void afterPropertiesSet() throws Exception { - Properties props=new Properties(); - props.setProperty("urule.repository.xml", "classpath:mysql.xml"); - setProperties(props); - } +@Deprecated +//@Configuration +public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer implements InitializingBean { + + public static final String SPRING_CONFIG_LOCATION = "spring.config.location"; + + @Autowired + private ResourceLoader resourceLoader; + + public void afterPropertiesSet() throws Exception { + // 外部配置定位 + List resources = new ArrayList<>(); + String springConfigLocation = System.getProperty(SPRING_CONFIG_LOCATION); + if (springConfigLocation != null) { + String[] configFiles = springConfigLocation.split(",|;"); + + for (String configFile : configFiles) { + resources.add(resourceLoader.getResource(configFile)); + } + } else { + resources.add(new ClassPathResource("application.properties")); + } + + // 配置加载并覆盖 urule-console 与 urule-core 中的配置项 + List propertiesList = new ArrayList<>(); + for (Resource resource : resources) { + try { + if (resource.exists()) { + Properties properties = new Properties(); + properties.load(resource.getInputStream()); + propertiesList.add(properties); + } + } catch (IOException e) { + logger.error("extra properties init failed", e); + } + } + super.setPropertiesArray(propertiesList.toArray(new Properties[propertiesList.size()])); + } } diff --git a/urule-springboot/src/main/java/com/bstek/urule/springboot/SpringBootPropertyPlaceholderAutoConfiguration.java b/urule-springboot/src/main/java/com/bstek/urule/springboot/SpringBootPropertyPlaceholderAutoConfiguration.java new file mode 100644 index 00000000..29690c72 --- /dev/null +++ b/urule-springboot/src/main/java/com/bstek/urule/springboot/SpringBootPropertyPlaceholderAutoConfiguration.java @@ -0,0 +1,25 @@ +package com.bstek.urule.springboot; + +import org.springframework.boot.autoconfigure.AutoConfigureOrder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.core.Ordered; + +/** + * @author Chery + * @date 2017/12/20 - 下午9:52 + */ +@Configuration +@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) +public class SpringBootPropertyPlaceholderAutoConfiguration { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer(); + placeholderConfigurer.setOrder(100); + placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true); + return placeholderConfigurer; + } + +} diff --git a/urule-springboot/src/main/resources/application-dev.properties b/urule-springboot/src/main/resources/application-dev.properties new file mode 100644 index 00000000..b90cc47b --- /dev/null +++ b/urule-springboot/src/main/resources/application-dev.properties @@ -0,0 +1,2 @@ +urule.repository.dir=d:/repo +urule.console.title=URule Dev \ No newline at end of file diff --git a/urule-springboot/src/main/resources/application-pro.properties b/urule-springboot/src/main/resources/application-pro.properties new file mode 100644 index 00000000..f63ceb3b --- /dev/null +++ b/urule-springboot/src/main/resources/application-pro.properties @@ -0,0 +1,2 @@ +urule.repository.xml=classpath:mysql.xml +urule.console.title=URule Pro \ No newline at end of file diff --git a/urule-springboot/src/main/resources/application.properties b/urule-springboot/src/main/resources/application.properties index cae354c7..a9095120 100644 --- a/urule-springboot/src/main/resources/application.properties +++ b/urule-springboot/src/main/resources/application.properties @@ -1 +1,2 @@ -urule.repository.dir=d:/repo \ No newline at end of file +# 可自由切换 profile 适配不同的环境 +spring.profiles.active=dev \ No newline at end of file diff --git a/urule-springboot/src/main/resources/mysql.xml b/urule-springboot/src/main/resources/mysql.xml new file mode 100644 index 00000000..16f0bf4b --- /dev/null +++ b/urule-springboot/src/main/resources/mysql.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file