From a4b2882a7127edcffc71ee5660419f6de87706ce Mon Sep 17 00:00:00 2001 From: Chery Date: Thu, 14 Dec 2017 22:22:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20SpringBoot=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8A=A0=E8=BD=BD=E6=96=B9=E5=BC=8F=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=9C=A8=E4=BB=A3=E7=A0=81=E4=B8=AD=E5=86=99=E6=AD=BB?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE=EF=BC=8C=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot/PropertiesConfiguration.java | 70 ++++++++++++++++--- 1 file changed, 59 insertions(+), 11 deletions(-) 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..d28b2e2c 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,69 @@ package com.bstek.urule.springboot; -import java.util.Properties; - +import com.bstek.urule.URulePropertyPlaceholderConfigurer; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.util.ResourceUtils; -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日 */ -//@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); - } +@Configuration +public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer implements InitializingBean { + + public static final String SPRING_CONFIG_LOCATION = "spring.config.location"; + + @Override + 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) { + String trimConfigFile = StringUtils.trim(configFile); + + if (trimConfigFile.startsWith(ResourceUtils.FILE_URL_PREFIX)) { + resources.add(new FileSystemResource(trimConfigFile)); + } else if (trimConfigFile.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) { + resources.add(new ClassPathResource(trimConfigFile)); + } else { + Resource resource = new FileSystemResource(trimConfigFile); + if (!resource.exists()) { + resource = new ClassPathResource(trimConfigFile); + } + resources.add(resource); + } + } + } 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) { + e.printStackTrace(); + } + } + super.setPropertiesArray(propertiesList.toArray(new Properties[propertiesList.size()])); + } } From 792ab370a110fde14de8624021b63562c100d827 Mon Sep 17 00:00:00 2001 From: Chery Date: Wed, 20 Dec 2017 20:34:40 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20ResourceLoader=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E5=8A=A0=E8=BD=BD=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot/PropertiesConfiguration.java | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) 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 d28b2e2c..bb0671f3 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,13 +1,12 @@ package com.bstek.urule.springboot; import com.bstek.urule.URulePropertyPlaceholderConfigurer; -import org.apache.commons.lang.StringUtils; 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.FileSystemResource; import org.springframework.core.io.Resource; -import org.springframework.util.ResourceUtils; +import org.springframework.core.io.ResourceLoader; import java.io.IOException; import java.util.ArrayList; @@ -23,9 +22,10 @@ public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer public static final String SPRING_CONFIG_LOCATION = "spring.config.location"; - @Override - public void afterPropertiesSet() throws Exception { + @Autowired + private ResourceLoader resourceLoader; + public void afterPropertiesSet() throws Exception { // 外部配置定位 List resources = new ArrayList<>(); String springConfigLocation = System.getProperty(SPRING_CONFIG_LOCATION); @@ -33,19 +33,7 @@ public void afterPropertiesSet() throws Exception { String[] configFiles = springConfigLocation.split(",|;"); for (String configFile : configFiles) { - String trimConfigFile = StringUtils.trim(configFile); - - if (trimConfigFile.startsWith(ResourceUtils.FILE_URL_PREFIX)) { - resources.add(new FileSystemResource(trimConfigFile)); - } else if (trimConfigFile.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) { - resources.add(new ClassPathResource(trimConfigFile)); - } else { - Resource resource = new FileSystemResource(trimConfigFile); - if (!resource.exists()) { - resource = new ClassPathResource(trimConfigFile); - } - resources.add(resource); - } + resources.add(resourceLoader.getResource(configFile)); } } else { resources.add(new ClassPathResource("application.properties")); @@ -61,7 +49,7 @@ public void afterPropertiesSet() throws Exception { propertiesList.add(properties); } } catch (IOException e) { - e.printStackTrace(); + logger.error("extra properties init failed", e); } } super.setPropertiesArray(propertiesList.toArray(new Properties[propertiesList.size()])); From 8b364c7512e94b997604bb19375b6ff8521bbeac Mon Sep 17 00:00:00 2001 From: Chery Date: Wed, 20 Dec 2017 23:30:54 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=85=BC=E5=AE=B9=20SpringBoot=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8A=A0=E8=BD=BD=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E6=94=B9=E5=8F=98=20SpringBoot=20=E7=9A=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E5=A6=82=EF=BC=9Aprofile=20?= =?UTF-8?q?=E7=AD=89=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- urule-springboot/pom.xml | 5 ++ .../springboot/PropertiesConfiguration.java | 4 +- ...tPropertyPlaceholderAutoConfiguration.java | 25 ++++++++++ .../main/resources/application-dev.properties | 2 + .../main/resources/application-pro.properties | 2 + .../src/main/resources/application.properties | 3 +- urule-springboot/src/main/resources/mysql.xml | 50 +++++++++++++++++++ 7 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 urule-springboot/src/main/java/com/bstek/urule/springboot/SpringBootPropertyPlaceholderAutoConfiguration.java create mode 100644 urule-springboot/src/main/resources/application-dev.properties create mode 100644 urule-springboot/src/main/resources/application-pro.properties create mode 100644 urule-springboot/src/main/resources/mysql.xml 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 bb0671f3..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 @@ -16,8 +16,10 @@ /** * @author Jacky.gao * @since 2016年10月12日 + * @see com.bstek.urule.springboot.SpringBootPropertyPlaceholderAutoConfiguration */ -@Configuration +@Deprecated +//@Configuration public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer implements InitializingBean { public static final String SPRING_CONFIG_LOCATION = "spring.config.location"; 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