SpringBoot實戰:SpringBoot之本地配置(二)

SpringBoot不僅僅只支持@value的形式讀取配置,還能夠同過spring管理的bean上添加@ConfigurationProperties來獲取。接下來咱們看看如何使用@ConfigurationProperties讀取配置值的。html

在application.properties文件中添加如下配置,固然在application-dev.properties文件中添加也是能夠的。java

#應用id
spring.application.id=spring-boot-wusy-demo
#應用名稱
spring.application.name=spring-boot-wusy-demo

#編碼設置
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true
spring.messages.encoding=UTF-8

# tomcat 配置
server.tomcat.accept-count=1000
server.tomcat.max-threads=500
# session超時時間,單位是秒
server.servlet.session.timeout=1800

#當前應用http端口
server.port=8787
#訪問地址,該配置能夠不配置,則直接經過ip+port訪問
#server.servlet.context-path=/demo

spring.profiles.active = dev

建立一個SpringApplicationProperties類,來獲取spring.application開頭的配置git

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author wusy
 * Company: xxxxxx科技有限公司
 * Createtime : 2020/2/25 22:57
 * Description : 讀取spring.application開頭的配置值
 */
@Configuration
@ConfigurationProperties(prefix = "spring.application")
@Setter
@Getter
public class SpringApplicationProperties {
    /**
     * 應用id
     */
    private String id ;
    /**
     * 應用名稱
     */
    private String name;
}

這裏引入了lombok開發輔助包,有關idea安裝lombok包的插件能夠參考https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html這裏就很少說了。這裏要主要這個類要添加@Configuration註解,讓spring在掃描的時候會掃描到該類,這裏須要提到的一點是SpringBoot的默認掃描路徑是啓動類所在的package,固然也可經過在啓動類中添加@ComponentScan來進行其餘路徑的掃描。web

引入pom.xml中引入spring-boot-configuration-processor包spring

<?xml version="1.0" encoding="UTF-8"?>
<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.wusy.demo</groupId>
    <artifactId>spring-boot-wusy-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 繼承SpringBoot父包 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath></relativePath>
    </parent>

    <dependencies>
        <!-- 引入lombok,方便開發 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- spring boot web支持:mvc,aop... -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

最後經過@Autowired自動注入SpringApplicationProperties類,來使用配置信息apache

import com.wusy.demo.config.SpringApplicationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wusy
 * Company: xxxxxx科技有限公司
 * Createtime : 2020/2/24 21:54
 * Description :
 */
@RestController
@RequestMapping("/api/demo")
public class HelloWorldController {

    @Autowired
    private SpringApplicationProperties springApplicationProperties;


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello world," + springApplicationProperties.getName();
    }
}

打開瀏覽器,在地址欄輸入http://127.0.0.1:8787/api/demo/helloapi

至此,SpringBoot經過@ConfigurationProperties來獲取配置信息例子演示完畢。瀏覽器

項目碼雲地址:https://gitee.com/wusycode/spring-boot-wusy-demo.gittomcat

相關文章
相關標籤/搜索