使用开发工具创建一个纯净maven工程
<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>
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
}
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;
}
}
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);
}
}
Run AS > Spring Boot App
http://localhost:8080/getOrder
{"orderId":"orderId_111","orderPrice":100,"createTime":"2022-04-23 08:10:51","extendList":null,"extendMap":null}
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;
}
}
# 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>
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);
}
}
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;
}
}
http://localhost:8080/createOrder
{"orderId":"orderId_222","orderPrice":200,"createTime":"2022-04-23 08:36:03","extendList":null,"extendMap":null}
删除配置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();
}
}
http://localhost:8080/createOrder
{"orderId":"orderId_222","orderPrice":200,"createTime":"2022-04-23 09:15:03","extendList":null,"extendMap":null}
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;
}
}
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();
}
}
http://localhost:8080/createOrder
{"orderId":"orderId_BBB","orderPrice":200,"createTime":"2022-04-23 09:40:13","extendList":null,"extendMap":null}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
# 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"}
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-------------------------
}
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;
}
}
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}
# 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"}
@PropertySource(value = { "order.properties" })
@Component
@ConfigurationProperties(prefix = "order")
public class OrderInfoModelV2 {
}
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}
# 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"}'
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}
本文梳理了SpringBoot六种读取配置方式,大家在项目中可以进行灵活组合和应用,希望本文对大家有所帮助。