Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

支持spring boot里的@ConfigurationProperties

hengyunabc edited this page May 1, 2016 · 2 revisions

在spring boot里,可以这样子配置bean:

@Configuration
@ConfigurationProperties(prefix = "memcached")
@EnableConfigurationProperties
public class ClientExampleAnnotationConfig {
	String serverlist;
	public String getServerlist() {
		return serverlist;
	}
	public void setServerlist(String serverlist) {
		this.serverlist = serverlist;
	}
}

当配置Properties里有memcached.serverlist=localhost:11211 这样子的配置,则会自动注入到serverlist这个field里。

在Xidamond client里提供了很方便的支持。只需要配置下面两个bean,就可以直接支持spring boot的基于前缀注入的功能了。

详细的例子可以参考examples里面的io.github.xdiamond.springboot.example.DemoApplication

	<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>
		<property name="bSyncToSystemProperties" value="true"></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>