springboot学习总结外部配置(命令行参数配置常规属性配置类型安全的配置之基于properties)

Posted vincentren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }

}

 


以上是关于springboot学习总结外部配置(命令行参数配置常规属性配置类型安全的配置之基于properties)的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot --外部配置的属性使用

SpringBoot外部配置夹加载顺序

3springboot:springboot配置文件(外部配置加载顺序自动配置原理,@Conditional)

Spring Boot2(011):外部化配置 Externalized Configuration

Spring Boot配置文件放在jar外部

Spring Boot配置文件放在jar外部