forked from hengyunabc/xdiamond
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add PropertySourcesAdderBean to support spring boot
- Loading branch information
1 parent
e411cab
commit d3a9c8d
Showing
5 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...t-example/src/main/java/io/github/xdiamond/springboot/example/PrefixAnnotationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.xdiamond.springboot.example; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* 演示spring boot里的利用前缀来注入的特性 | ||
* | ||
* @author hengyunabc | ||
* | ||
*/ | ||
@Configuration | ||
@ConfigurationProperties(prefix = "memcached") | ||
@EnableConfigurationProperties | ||
public class PrefixAnnotationConfig { | ||
String serverlist; | ||
|
||
public String getServerlist() { | ||
return serverlist; | ||
} | ||
|
||
public void setServerlist(String serverlist) { | ||
this.serverlist = serverlist; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PrefixAnnotationConfig [serverlist=" + serverlist + "]"; | ||
} | ||
|
||
} |
14 changes: 11 additions & 3 deletions
14
...ent-example/src/main/java/io/github/xdiamond/springprofile/example/SpringProfileMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
xdiamond-client-example/src/main/resources/spring-context-springboot-example.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" | ||
xmlns:util="http://www.springframework.org/schema/util" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context | ||
http://www.springframework.org/schema/context/spring-context.xsd | ||
http://www.springframework.org/schema/util | ||
http://www.springframework.org/schema/util/spring-util.xsd"> | ||
|
||
<bean id="xDiamondConfig" | ||
class="io.github.xdiamond.client.spring.XDiamondConfigFactoryBean"> | ||
<property name="serverHost" value="${xdiamond.server.host:localhost}" /> | ||
<property name="serverPort" value="5678" /> | ||
<property name="groupId" value="io.github.xdiamond" /> | ||
<property name="artifactId" value="xdiamond-client-example" /> | ||
<property name="version" value="0.0.1-SNAPSHOT" /> | ||
<property name="profile" value="${xdiamond.project.profile:dev}" /> | ||
<property name="secretKey" value="${xdiamond.project.secretkey:123456}"></property> | ||
</bean> | ||
|
||
<bean class="io.github.xdiamond.client.spring.PropertySourcesAdderBean"> | ||
<property name="properties"> | ||
<bean class="java.util.Properties" factory-bean="xDiamondConfig" | ||
factory-method="getProperties"> | ||
</bean> | ||
</property> | ||
</bean> | ||
|
||
<context:component-scan base-package="io.github.xdiamond.example"> | ||
</context:component-scan> | ||
|
||
<bean id="clientExampleConfig" class="io.github.xdiamond.example.ClientExampleConfig"> | ||
<property name="memcachedAddress" value="${memcached.serverlist}"></property> | ||
<property name="zookeeperAddress" value="${zookeeper.address}"></property> | ||
</bean> | ||
|
||
<bean class="io.github.xdiamond.example.listener.ListenerXmlBean"></bean> | ||
|
||
</beans> |
80 changes: 80 additions & 0 deletions
80
xdiamond-client/src/main/java/io/github/xdiamond/client/spring/PropertySourcesAdderBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.github.xdiamond.client.spring; | ||
|
||
import java.util.Properties; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.core.PriorityOrdered; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.env.PropertiesPropertySource; | ||
|
||
/** | ||
* | ||
* 把 properties对象加到spring的 {@link org.springframework.core.env.PropertySources} | ||
* 里。 | ||
* | ||
* @author hengyunabc | ||
* | ||
* @see org.springframework.core.env.PropertySources | ||
* @see org.springframework.core.env.ConfigurableEnvironment | ||
*/ | ||
public class PropertySourcesAdderBean | ||
implements InitializingBean, ApplicationContextAware, PriorityOrdered, BeanFactoryPostProcessor { | ||
|
||
private String name = "xdiamond"; | ||
|
||
private Properties properties; | ||
|
||
private ApplicationContext applicationContext; | ||
|
||
public PropertySourcesAdderBean() { | ||
|
||
} | ||
|
||
public void afterPropertiesSet() throws Exception { | ||
PropertiesPropertySource propertySource = new PropertiesPropertySource(name, this.properties); | ||
|
||
Environment environment = applicationContext.getEnvironment(); | ||
if (environment instanceof ConfigurableEnvironment) { | ||
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) applicationContext | ||
.getEnvironment(); | ||
configurableEnvironment.getPropertySources().addLast(propertySource); | ||
} | ||
} | ||
|
||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | ||
|
||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Properties getProperties() { | ||
return properties; | ||
} | ||
|
||
public void setProperties(Properties properties) { | ||
this.properties = properties; | ||
} | ||
|
||
} |