xml 方式
1、在resources/spring里面新建applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用xDiamond统一配置中心,服务启动时会到10.0.200.53 根据groupId,project.artifactId拉取项目的配置 -->
<bean id="xDiamondConfig"
class="io.github.xdiamond.client.spring.XDiamondConfigFactoryBean">
<property name="serverHost" value="${xdiamond.server.host}"/>
<property name="serverPort" value=""/>
<property name="groupId" value=""/>
<property name="artifactId" value="${project.artifactId}"/>
<property name="version" value="${project.version:0.0.1-SNAPSHOT}"/>
<property name="profile" value="${project.profile:dev}"/>
<!-- <property name="secretKey" value="${xdiamond.project.secretkey:123456}"></property> -->
<property name="locations">
<list>
<value>classpath:property-placeholder.properties</value> <!--本地配置 -->
</list>
</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>
</beans>
2、在resources里面添加properties-placeholder.properties文件
xdiamond.server.host=
project.artifactId=
project.version=0.0.1-SNAPSHOT
project.profile=
3、添加配置类,同时在主类上添加注解
配置类通过@Value的方式注入配置,在主类上添加@ImportResource(locations = { "classpath*:/spring/applicationContext.xml"})
登陆发表评论