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

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

mapping span ppi oid res component cati void auth

學習的內容主要是汪雲飛的《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);
    }

}

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