1.新建maven項目, 在pom.xml中添加 profile節點信息以下:spring
<profiles> <profile> <!-- 開發環境 --> <id>dev</id> <properties> <environment>development</environment><!-- 節點名字environment是本身隨意取的 --> </properties> <activation> <activeByDefault>true</activeByDefault><!-- 默認激活該profile節點--> </activation> </profile> <profile> <!-- 測試環境 --> <id>test</id> <properties> <environment>test</environment> </properties> </profile> <profile> <!-- 預演環境 --> <id>prev</id> <properties> <environment>preview</environment> </properties> </profile> <profile> <!-- 生產環境 --> <id>prod</id> <properties> <environment>production</environment> </properties> </profile> </profiles>
2. 在項目中添加各環境須要的數據源配置文件,分不一樣目錄存放, 分別是開發,測試, 預演,生產 環境. 以下圖左邊部分所示. maven
3. pom.xml中配置resource節點信息.測試
<build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>environment/development/*</exclude> <exclude>environment/test/*</exclude> <exclude>environment/preview/*</exclude> <exclude>environment/production/**</exclude> </excludes> </resource> <resource> <directory>src/main/resources/environment/${environment}</directory> <targetPath>environment/${environment}</targetPath> </resource> </resources> </build>
resource節點信息解釋: ui
<directory>src/main/resources</directory> <!--打包時包含src/main/resources目錄下全部"子"文件 和 "孫"文件.如config 和environment --> <exclude>environment/development/**</exclude> <!--打包時排除src/main/resources/environment/development下全部"子"文件 和 "孫"文件.--> <exclude>environment/test/**</exclude> <!--打包時排除src/main/resources/environment/test下全部"子"文件 和 "孫"文件.--> <exclude>environment/preview/**</exclude> <!--打包時排除src/main/resources/environment/preview下全部"子"文件 和 "孫"文件.--> <exclude>environment/production/**</exclude><!--打包時排除src/main/resources/environment/production下全部"子"文件 和 "孫"文件.--> <!-- 注意點: 若是寫一個心號*, 如<exclude>environment/development/*</exclude> 則表示:打包時排除src/main/resources/environment/development下全部"子"文件, 不排除"孫"文件, 如上一個截圖所示.--> <!-- 以上配置優先度從上到下 遞增, 這就達到目的: config目錄下的配置各環境都須要, 而其它環境相關的配置只會有一個目錄被打包--->
<directory>src/main/resources/environment/${environment}</directory> <!-- 打包時包含src/main/resources/environment/${environment}下全部"子"文件,environment變量值來自profile中賦值 --> <targetPath>environment/${environment}</targetPath><!--指定src/main/resources/environment/${environment}全部"子文件" 打包 到包的哪一個目錄 -->
4. 項目打包. 打包命令mvn package -Pdev來指定激活id爲 dev 的profile節點, 這樣, 開發環境配置文件就會被打包.
開發: mvn package -Pdev (由於配置了默認激活dev部分, 因此也能夠使用mvn package, 這與 mvn package -Pdev 效果相同)
測試: mvn package -Ptest
預演:mvn package -Pprev
生產:mvn package -Pprodthis
除此方法外動態的指定配置文件咱們還能夠使用ClassPathXmlApplicationContextcode
1.建立一個xml文件config.xmlserver
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--配置文件對象,id是惟一標識,class是對應的配置類,property是配置字段--> <bean id="test" class="flink_job.omega_navigation_filter.object.Config"> <property name="source_kafka_topic_1" value="topic1" /> <property name="source_kafka_server_1" value="bigdata-swan:9092" /> <property name="source_kafka_group_id_1" value="ora_log_20171031" /> <property name="source_kafka_offset_1" value="latest" /> <property name="output_kafka_topic" value="w_omega_stat_native_fmt" /> <property name="output_kafka_server" value="bigdata-swan:9092" /> </bean> <bean id="local" class="flink_job.omega_navigation_filter.object.Config"> <property name="source_kafka_topic_1" value="topic1" /> <property name="source_kafka_server_1" value="bigdata-swan:9092" /> <property name="source_kafka_group_id_1" value="ora_log_20171031" /> <property name="source_kafka_offset_1" value="latest" /> <property name="output_kafka_topic" value="w_omega_stat_native_fmt" /> <property name="output_kafka_server" value="bigdata-swan:9092" /> </bean> </beans>
2.建立對應的配置類configxml
public class Config implements Serializable{ private static final long serialVersionUID = 1L; private String source_kafka_topic_1; private String source_kafka_server_1; private String source_kafka_group_id_1; private String source_kafka_offset_1; private String source_kafka_topic_2; private String source_kafka_server_2; private String source_kafka_group_id_2; private String source_kafka_offset_2; private String output_kafka_topic; private String output_kafka_server; public static long getSerialversionuid() { return serialVersionUID; } public String getSource_kafka_topic_1() { return source_kafka_topic_1; } public void setSource_kafka_topic_1(String source_kafka_topic_1) { this.source_kafka_topic_1 = source_kafka_topic_1; } public String getSource_kafka_server_1() { return source_kafka_server_1; } public void setSource_kafka_server_1(String source_kafka_server_1) { this.source_kafka_server_1 = source_kafka_server_1; } public String getSource_kafka_group_id_1() { return source_kafka_group_id_1; } public void setSource_kafka_group_id_1(String source_kafka_group_id_1) { this.source_kafka_group_id_1 = source_kafka_group_id_1; } public String getSource_kafka_offset_1() { return source_kafka_offset_1; } public void setSource_kafka_offset_1(String source_kafka_offset_1) { this.source_kafka_offset_1 = source_kafka_offset_1; } public String getSource_kafka_topic_2() { return source_kafka_topic_2; } public void setSource_kafka_topic_2(String source_kafka_topic_2) { this.source_kafka_topic_2 = source_kafka_topic_2; } public String getSource_kafka_server_2() { return source_kafka_server_2; } public void setSource_kafka_server_2(String source_kafka_server_2) { this.source_kafka_server_2 = source_kafka_server_2; } public String getSource_kafka_group_id_2() { return source_kafka_group_id_2; } public void setSource_kafka_group_id_2(String source_kafka_group_id_2) { this.source_kafka_group_id_2 = source_kafka_group_id_2; } public String getSource_kafka_offset_2() { return source_kafka_offset_2; } public void setSource_kafka_offset_2(String source_kafka_offset_2) { this.source_kafka_offset_2 = source_kafka_offset_2; } public String getOutput_kafka_topic() { return output_kafka_topic; } public void setOutput_kafka_topic(String output_kafka_topic) { this.output_kafka_topic = output_kafka_topic; } public String getOutput_kafka_server() { return output_kafka_server; } public void setOutput_kafka_server(String output_kafka_server) { this.output_kafka_server = output_kafka_server; } }
3.程序中指定調用的配置對象的id對象
public static ApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml"); public static Config co = (Config) ctx.getBean("test");
這樣咱們就能獲取對應的字段開發