增長git
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
修改後的 pom.xml 以下web
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xnx3.springcloud</groupId> <artifactId>springcloud-main</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.xnx3.springcloud.config.client.test</groupId> <artifactId>springcloud-config-client-test</artifactId> <!-- 如下是增長的 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 以上是增長的 --> </project>
package com.xnx3.springcloud.config.client.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
會將 git 遠程配置文件 author.properties 的配置信息自動加載到此類。使用時,直接取spring
package com.xnx3.springcloud.config.client.test.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "com.xnx3.author") public class AuthorConfig { private String name; private Integer age; private String site; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSite() { return site; } public void setSite(String site) { this.site = site; } @Override public String toString() { return "AuthorConfig [name=" + name + ", age=" + age + ", site=" + site + "]"; } }
package com.xnx3.springcloud.config.client.test.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.xnx3.springcloud.config.client.test.config.AuthorConfig; @Controller @RequestMapping("config") public class ConfigTestController { @Autowired private AuthorConfig authorConfig; @RequestMapping("/getAuthor") @ResponseBody public String getAuthorInfo(){ return " author信息是叢git上加載下來的 :" + authorConfig.toString(); } }
。。。apache