Spring boot 引用自定義配置文件
阿新 • • 發佈:2019-01-28
npr urn str spro tle control ons void mpi
依賴jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.0.5.RELEASE</version> </dependency>
示例如下:
1. 新建 Maven 項目 properties
2. pom.xml
<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> <groupId>com.java</groupId> <artifactId>properties</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
3. 配置文件 user-defined.properties
aaa.url.login=aaa-login.html
aaa.url.order=aaa-order.html
bbb.goods.price=500
bbb.goods.weight=1000
4. PropertiesStarter.java
package com.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 主啟動類 * * @author Storm * */ @SpringBootApplication public class PropertiesStarter { public static void main(String[] args) { SpringApplication.run(PropertiesStarter.class, args); } }
5. URLProperties.java
package com.java.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import lombok.Data; /** * 前綴為aaa.url的配置自動註入到對應屬性中 * * @author Storm * */ @Data @ConfigurationProperties(prefix = "aaa.url") public class URLProperties { private String login; private String order; }
6. GoodsProperties.java
package com.java.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import lombok.Data; /** * 前綴為bbb.goods的配置自動註入到對應屬性中 * * @author Storm * */ @Data @ConfigurationProperties(prefix = "bbb.goods") public class GoodsProperties { private String price; private String weight; }
7. PropertiesConfig.java
package com.java.config; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.java.properties.GoodsProperties; import com.java.properties.URLProperties; /** * 用戶自定義配置文件配置類 * * @author Storm * */ @Configuration @PropertySource({ "classpath:user-defined.properties" }) @EnableConfigurationProperties({ URLProperties.class, GoodsProperties.class }) public class PropertiesConfig { }
8. DemoController.java
package com.java.controller; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.java.properties.GoodsProperties; import com.java.properties.URLProperties; @RestController public class DemoController { @Autowired private URLProperties url; @Autowired private GoodsProperties goods; @GetMapping("/getProperties") public Map<String, Object> getProperties() { System.out.println(url.getOrder()); Map<String, Object> map = new HashMap<>(); map.put("url", url); map.put("goods", goods); return map; } }
9. 運行 PropertiesStarter.java ,啟動測試
瀏覽器輸入 http://localhost:8080/getProperties
返回結果如下:
{"goods":{"price":"500","weight":"1000"},"url":{"login":"aaa-login.html","order":"aaa-order.html"}}
配置加載成功!
.
Spring boot 引用自定義配置文件