1 SpringBoot工程創建
1.1 maven工程創建
使用開發工具創建一個純凈maven工程
1.2 引入依賴
<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.front.spring.boot</groupId>
<artifactId>spring-boot-java-front</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
1.3 新增訂單模型
package com.java.front.spring.boot.model;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonFormat;
public class OrderInfoModel {
private String orderId;
private Integer orderPrice;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
private List<String> extendList;
private Map<String, String> extendMap;
// getter setter
}
1.4 新增訪問端點
package com.java.front.spring.boot.controller;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMApping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java.front.spring.boot.model.OrderInfoModel;
@Controller
public class OrderController {
@ResponseBody
@RequestMapping("getOrder")
public OrderInfoModel queryOrder() {
OrderInfoModel orderInfo = new OrderInfoModel();
orderInfo.setOrderId("orderId_111");
orderInfo.setOrderPrice(100);
orderInfo.setCreateTime(new Date());
return orderInfo;
}
}
1.5 創建啟動類
package com.java.front.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JavaFrontApplication {
public static void main(String[] args) {
SpringApplication.run(JavaFrontApplication.class, args);
}
}
1.6 運行啟動類
Run AS > Spring Boot App
1.7 訪問測試
http://localhost:8080/getOrder
{"orderId":"orderId_111","orderPrice":100,"createTime":"2022-04-23 08:10:51","extendList":null,"extendMap":null}
2 方式一:XML
2.1 新增訂單服務
package com.java.front.spring.boot.service;
import com.java.front.spring.boot.model.OrderInfoModel;
public interface OrderService {
public OrderInfoModel createOrder();
}
package com.java.front.spring.boot.service;
import java.util.Date;
import com.java.front.spring.boot.model.OrderInfoModel;
public class OrderServiceImpl implements OrderService {
@Override
public OrderInfoModel createOrder() {
String orderId = "orderId_222";
OrderInfoModel orderInfo = new OrderInfoModel();
orderInfo.setOrderId(orderId);
orderInfo.setOrderPrice(200);
orderInfo.setCreateTime(new Date());
return orderInfo;
}
}
2.2 新增配置文件
# src/main/resources/spring-biz.xml
<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">
<bean id="orderService" class="com.java.front.spring.boot.service.OrderServiceImpl" />
</beans>
2.3 啟動類引入資源文件
package com.java.front.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = { "classpath:spring-biz.xml" })
@SpringBootApplication
public class JavaFrontApplication {
public static void main(String[] args) {
SpringApplication.run(JavaFrontApplication.class, args);
}
}
2.4 新增訪問端點
package com.java.front.spring.boot.controller;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java.front.spring.boot.model.OrderInfoModel;
import com.java.front.spring.boot.service.OrderService;
@Controller
public class OrderController {
@Resource
private OrderService orderService;
@ResponseBody
@RequestMapping("createOrder")
public OrderInfoModel createOrder() {
OrderInfoModel orderInfo = orderService.createOrder();
return orderInfo;
}
}
2.5 啟動并訪問
http://localhost:8080/createOrder
{"orderId":"orderId_222","orderPrice":200,"createTime":"2022-04-23 08:36:03","extendList":null,"extendMap":null}
3 方式二:@Bean
3.1 新增配置類
刪除配置spring-biz.xml并且新增如下配置:
package com.java.front.spring.boot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.java.front.spring.boot.service.OrderService;
import com.java.front.spring.boot.service.OrderServiceImpl;
@Configuration
public class OrderServiceConfig {
@Bean
public OrderService orderService() {
return new OrderServiceImpl();
}
}
3.2 啟動并訪問
http://localhost:8080/createOrder
{"orderId":"orderId_222","orderPrice":200,"createTime":"2022-04-23 09:15:03","extendList":null,"extendMap":null}
4 方式三:@Bean增強
4.1 新增訂單服務
package com.java.front.spring.boot.service;
import java.util.Date;
import com.java.front.spring.boot.model.OrderInfoModel;
public class OrderServiceAImpl implements OrderService {
@Override
public OrderInfoModel createOrder() {
String orderId = "orderId_AAA";
OrderInfoModel orderInfo = new OrderInfoModel();
orderInfo.setOrderId(orderId);
orderInfo.setOrderPrice(200);
orderInfo.setCreateTime(new Date());
return orderInfo;
}
}
package com.java.front.spring.boot.service;
import java.util.Date;
import org.springframework.stereotype.Service;
import com.java.front.spring.boot.model.OrderInfoModel;
@Service
public class OrderServiceBImpl implements OrderService {
@Override
public OrderInfoModel createOrder() {
String orderId = "orderId_BBB";
OrderInfoModel orderInfo = new OrderInfoModel();
orderInfo.setOrderId(orderId);
orderInfo.setOrderPrice(200);
orderInfo.setCreateTime(new Date());
return orderInfo;
}
}
4.2 修改配置類
package com.java.front.spring.boot.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.java.front.spring.boot.service.OrderService;
import com.java.front.spring.boot.service.OrderServiceAImpl;
@Configuration
public class OrderServiceConfig {
/**
* 默認情況使用此實例
*
* 如果容器有其它實例則使用其它實例
*/
@Bean
@ConditionalOnMissingBean(OrderService.class)
public OrderService orderService() {
return new OrderServiceAImpl();
}
}
4.3 啟動并訪問
http://localhost:8080/createOrder
{"orderId":"orderId_BBB","orderPrice":200,"createTime":"2022-04-23 09:40:13","extendList":null,"extendMap":null}
5 方式四:application.properties
5.1 引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
5.2 新增配置文件
# src/main/resources/application.properties
server.port=9999
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
order.orderId=orderId_abc
order.orderPrice=500
order.createTime=2022/01/01 11:00:00
order.extendList=a,b,c
order.extendMap.k1=v1
order.extendMap.k2=v2
java.front.test.boolean=true
java.front.test.list=a,b,c
java.front.test.map={"k1":"v1","k2":"v2"}
5.3 新增訂單模型
package com.java.front.spring.boot.model;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "order")
public class OrderInfoModelV2 {
// ---------application.properties start with order config---------
private String orderId;
private Integer orderPrice;
private Date createTime;
private List<String> extendList;
private Map<String, String> extendMap;
// ---------application.properties use @value to read-------------
@Value("${java.front.test.boolean:false}")
private Boolean testBoolean;
@Value("#{'${java.front.test.list:{}}'.split(',')}")
private List<String> testList;
@Value("#{${java.front.test.map:null}}")
private Map<String, String> testMap;
@Value("#{3*10}")
private Integer testInteger;
// -------------------------getter setter-------------------------
}
5.4 新增訪問端點
package com.java.front.spring.boot.controller;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java.front.spring.boot.model.OrderInfoModel;
import com.java.front.spring.boot.model.OrderInfoModelV2;
import com.java.front.spring.boot.service.OrderService;
@Controller
public class OrderController {
@Resource
private OrderInfoModelV2 orderInfoModelV2;
@ResponseBody
@RequestMapping("queryOrderFromConfig")
public OrderInfoModelV2 queryOrderFromConfig() {
return orderInfoModelV2;
}
}
5.5 啟動并訪問
http://localhost:9999/queryOrderFromConfig
{"orderId":"orderId_abc","orderPrice":500,"createTime":"2022-01-01 11:00:00","extendList":["a","b","c"],"extendMap":{"k1":"v1","k2":"v2"},"testBoolean":true,"testList":["a","b","c"],"testMap":{"k1":"v1","k2":"v2"},"testInteger":30}
6 方式五:@PropertySource
6.1 拆分訂單配置
# src/main/resources/order.properties
order.orderId=orderId_abc
order.orderPrice=500
order.createTime=2022/01/01 11:00:00
order.extendList=a,b,c
order.extendMap.k1=v1
order.extendMap.k2=v2
java.front.test.boolean=true
java.front.test.list=a,b,c
java.front.test.map={"k1":"v1","k2":"v2"}
6.2 @PropertySource
@PropertySource(value = { "order.properties" })
@Component
@ConfigurationProperties(prefix = "order")
public class OrderInfoModelV2 {
}
6.3 啟動并訪問
http://localhost:9999/queryOrderFromConfig
{"orderId":"orderId_abc","orderPrice":500,"createTime":"2022-01-01 11:00:00","extendList":["a","b","c"],"extendMap":{"k1":"v1","k2":"v2"},"testBoolean":true,"testList":["a","b","c"],"testMap":{"k1":"v1","k2":"v2"},"testInteger":30}
7 方式六:application.yaml
7.1 新增配置文件
# src/main/resources/application.yaml
server:
port: 9999
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
order:
orderId: orderId_abc
orderPrice: 500
createTime: 2022/01/01 11:00:00
extendList:
- a
- b
- c
extendMap:
k1: v1
k2: v2
java:
front:
test:
boolean: true
list: 'a,b,c'
map: '{"k1":"v1","k2":"v2"}'
7.2 啟動并訪問
http://localhost:9999/queryOrderFromConfig
{"orderId":"orderId_abc","orderPrice":500,"createTime":"2022-01-01 11:00:00","extendList":["a","b","c"],"extendMap":{"k1":"v1","k2":"v2"},"testBoolean":true,"testList":["a","b","c"],"testMap":{"k1":"v1","k2":"v2"},"testInteger":30}
8 文章總結
本文梳理了SpringBoot六種讀取配置方式,大家在項目中可以進行靈活組合和應用,希望本文對大家有所幫助。
歡迎大家關注今日頭條號「JAVA前線」查看更多精彩分享文章,主要包括源碼分析、實際應用、架構思維、職場分享、產品思考