Skip to content

Commit

Permalink
重构加载配置文件方式
Browse files Browse the repository at this point in the history
  • Loading branch information
pickear committed Oct 31, 2017
1 parent 020440c commit 8550dcd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 48 deletions.
79 changes: 79 additions & 0 deletions bow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 36,10 @@

<build>
<plugins>
<!--<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down Expand Up @@ -87,6 91,81 @@
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>package-jar2</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>
${env.JAVA_HOME}/bin/javapackager
</executable>
<arguments>
<argument>-Bruntime</argument>
<argument>${env.JAVA_HOME}</argument>
<argument>-deploy</argument>
<argument>-native</argument>
<argument>exe</argument>
<argument>-appclass</argument>
<argument>com.amor.bow.BowApplicationLaucher</argument>
&lt;!&ndash;<argument>-srcdir</argument>
<argument>${project.build.directory}</argument>
<argument>-srcfiles</argument>
<argument>${project.build.directory}\${artifactId}-${version}.jar</argument>&ndash;&gt;
<argument>-outdir</argument>
<argument>${project.build.directory}</argument>
<argument>-outfile</argument>
<argument>${project.artifactId}-${version}</argument>
<argument>-v</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>-->
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version> 2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.amor.bow.BowApplicationLaucher</mainClass>
</transformer>
<transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />
</transformers>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
</plugin>-->
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 10,7 @@
* Created by dylan on 2017/10/19.
*/
@RestController
public class BowController {

@RequestMapping("/")
@ResponseBody
public String http(){
return "http";
}
public class AdminController {

@RequestMapping("/reloadProperties")
@ResponseBody
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 1,23 @@
package com.amor.bow.listener;

import com.amor.bow.config.BowProxyProperties;
import com.amor.bow.helper.SpringBeanHolder;
import com.amor.bow.listener.event.PropertiesModifiedEvent;
import com.amor.bow.repository.impl.UserLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.validation.BindException;

import java.util.List;

/**
* Created by dell on 2017/10/25.
*/
public class PropertiesModifiedListener extends ConfigFileApplicationListener{
public class PropertiesModifiedListener implements ApplicationListener<PropertiesModifiedEvent> {

private final static Logger logger = LoggerFactory.getLogger(PropertiesModifiedListener.class);

private ApplicationEnvironmentPreparedEvent environmentPreparedEvent = null;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ApplicationEnvironmentPreparedEvent){
this.environmentPreparedEvent = (ApplicationEnvironmentPreparedEvent)event;
}
if(event instanceof PropertiesModifiedEvent){
logger.info("listen a prpperties modified event,reload the properties.");
AutowireCapableBeanFactory beanFactory = SpringBeanHolder.getContext()
.getAutowireCapableBeanFactory();

BowProxyProperties properties = SpringBeanHolder.getBean(BowProxyProperties.class);
if(null != properties){
beanFactory.destroyBean(properties);
}
postProcessEnvironment(environmentPreparedEvent.getEnvironment(),environmentPreparedEvent.getSpringApplication());
beanFactory.createBean(BowProxyProperties.class);
/*List<PropertiesConfigurationFactory> propertiesConfigurationFactories = SpringFactoriesLoader.
loadFactories(PropertiesConfigurationFactory.class,getClass().getClassLoader());
propertiesConfigurationFactories.forEach(factory ->{
try {
factory.bindPropertiesToTarget();
} catch (BindException e) {
e.printStackTrace();
}
});*/
}

public void onApplicationEvent(PropertiesModifiedEvent event) {
logger.info("listen properties modified event,reload it!");
UserLoader loader = SpringBeanHolder.getBean(UserLoader.class);
loader.reload();
}
}
6 changes: 3 additions & 3 deletions bow/src/main/resources/application-users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 16,9 @@ users:
remotePort: 1025
protocolType: TCP
- id: 3
mapIp: 127.0.0.1
mapPort: 3030
mapIp: 192.168.234.63
mapPort: 42080
remoteIp: 127.0.0.1
remotePort: 1025
subDomain: local
subDomain: locall
protocolType: HTTP

0 comments on commit 8550dcd

Please sign in to comment.