一文讲清楚SpringBoot六种读取配置方式

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 新增访问端点【一文讲清楚SpringBoot六种读取配置方式】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;@Controllerpublic 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;@SpringBootApplicationpublic 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 方式一:XML2.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 {@Overridepublic 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/beanshttp://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" })@SpringBootApplicationpublic 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;@Controllerpublic class OrderController {@Resourceprivate OrderService orderService;@ResponseBody@RequestMapping("createOrder")public OrderInfoModel createOrder() {OrderInfoModel orderInfo = orderService.createOrder();return orderInfo;}}


推荐阅读