Skip to content

Commit

Permalink
add PropertySourcesAdderBean to support spring boot
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyunabc committed Apr 30, 2016
1 parent e411cab commit d3a9c8d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.xdiamond.springboot.example;

import io.github.xdiamond.example.ClientExampleAnnotationConfig;
import io.github.xdiamond.example.listener.ListenerExampleService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -9,14 +10,19 @@
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ComponentScan("io.github.xdiamond.example")
@ImportResource("classpath:spring-context-clientexample.xml")
@ComponentScan(basePackages = { "io.github.xdiamond.example", "io.github.xdiamond.springboot.example" })
@ImportResource("classpath:spring-context-springboot-example.xml")
public class DemoApplication {

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

ClientExampleAnnotationConfig config = context.getBean(ClientExampleAnnotationConfig.class);
System.err.println(config);
}
ClientExampleAnnotationConfig config = context.getBean(ClientExampleAnnotationConfig.class);
System.err.println(config);

PrefixAnnotationConfig prefixAnnotationConfig = context.getBean(PrefixAnnotationConfig.class);

System.err.println(prefixAnnotationConfig);

}
}
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 + "]";
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package io.github.xdiamond.springprofile.example;

import io.github.xdiamond.example.ClientExampleAnnotationConfig;
import io.github.xdiamond.example.ClientExampleConfig;

import org.springframework.context.support.GenericXmlApplicationContext;

import io.github.xdiamond.example.ClientExampleConfig;

/**
* 演示利用spring profiles的特性,来切换不同环境的xdiamond client配置。
*
* 比如,在测试环境使用的xdiamond server和生产环境使用的xidamond server的域名不一样,则可以通过spring
* profiles很方便地切换。
*
* @author hengyunabc
*
*/
public class SpringProfileMain {
public static void main(String[] args) throws InterruptedException {
// 这些变量可以通过命令行或者在tomcat的setenv.sh文件里传递进来,这里简单起见,直接设置到system properties里
Expand Down
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>
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;
}

}

0 comments on commit d3a9c8d

Please sign in to comment.