欢迎我们关注大众号「JAVA前线」检查更多精彩共享文章,首要包括源码分析、实践使用、架构思想、职场共享、产品考虑等等,同时欢迎我们加我个人微信「java_front」一同交流学习

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前线」检查更多精彩共享文章,首要包括源码分析、实践使用、架构思想、职场共享、产品考虑等等,同时欢迎我们加我个人微信「java_front」一同交流学习