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

 
2.5 启动并访问http://localhost:8080/createOrder{"orderId":"orderId_222","orderPrice":200,"createTime":"2022-04-23 08:36:03","extendList":null,"extendMap":null} 
3 方式二:@Bean3.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;@Configurationpublic class OrderServiceConfig {@Beanpublic 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 {@Overridepublic 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;@Servicepublic class OrderServiceBImpl implements OrderService {@Overridepublic 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;@Configurationpublic 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.properties5.1 引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency> 
5.2 新增配置文件# src/main/resources/application.propertiesserver.port=9999spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.jackson.time-zone=GMT+8order.orderId=orderId_abcorder.orderPrice=500order.createTime=2022/01/01 11:00:00order.extendList=a,b,corder.extendMap.k1=v1order.extendMap.k2=v2java.front.test.boolean=truejava.front.test.list=a,b,cjava.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;@Controllerpublic class OrderController {@Resourceprivate OrderInfoModelV2 orderInfoModelV2;@ResponseBody@RequestMapping("queryOrderFromConfig")public OrderInfoModelV2 queryOrderFromConfig() {return orderInfoModelV2;}}


推荐阅读