1. 程式人生 > >springboot學習總結(一)外部配置(命令列引數配置、常規屬性配置、型別安全的配置之基於properties)

springboot學習總結(一)外部配置(命令列引數配置、常規屬性配置、型別安全的配置之基於properties)

學習的內容主要是汪雲飛的《Spring Boot實戰》

(一)命令列引數配置

springboot專案可以基於jar包執行,開啟jar的程式可以通過下面命令列執行:

java -jar xxx.jar

可以通過以下命令修改tomcat埠號

java -jar xxx.jar --server.port=9090

(二)常規屬性配置

在springboot專案中,我們只需在application.properties定義屬性,直接使用@Value注入即可

(1)application.properties中新增屬性

book.author=wangyunfei
book.name
=spring boot

(2)修改入口類

package com.vincent.demo;

import com.vincent.demo.config.Author;
import com.vincent.demo.config.HelloService;
import com.vincent.demo.config.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class DemoApplication { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @RequestMapping("/") String index(){ return "book name is :" + bookName + " and book author is: " + bookAuthor; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

(三)型別安全的配置(基於properties)

通過@ConfigurationProperties將properties屬性和一個Bean及其相關屬性關聯、從而實現型別安全的配置

(1)新增配置,在application.properties上新增:

author.name=wyf
author.age=32

(2)型別安全的Bean,程式碼如下:

package com.vincent.demo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author rw
 * @date 2018/12/17 下午9:50
 */
@Component
//載入properties檔案內的配置,通過prefix屬性指定properties的配置的字首,通過locations指定properties檔案的位置
@ConfigurationProperties(prefix = "author")
public class Author {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

(3)可以用@Autowired直接注入該配置

package com.vincent.demo;

import com.vincent.demo.config.Author;
import com.vincent.demo.config.HelloService;
import com.vincent.demo.config.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

    @Value("${book.author}")
    private String bookAuthor;

    @Value("${book.name}")
    private String bookName;

    @Autowired
    Author author;

    @RequestMapping("/")
    String index(){
        return "book name is :" + bookName + " and book author is: " + bookAuthor + " author.name is:" + author.getName();
    }public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}