Spring Boot一些基础配置

一、SpringBoot中Banner配置 

1.Spring Boot项目在启动的时候会有一个默认的会有Spring图案

2.我们可以把这个图案修改为自己想要的。在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。ASCII图案可通过网站http://www.network-science.de/ascii/一键生成,比如生成图案后复制到banner.txt,启动项目,idea控制台输出如下:

3.banner也可以关闭,在main方法中:

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(DemoApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

}

 

二、全局配置文件

 1.在src/main/resources目录下,Spring Boot提供了一个名为application.properties的全局配置文件,可对一些默认配置的配置值进行修改。

2.Spring Boot允许我们在application.properties下自定义一些属性,比如:

test.demo.name=zhangsan
test.demo.age=25

 3.定义一个TestConfig Bean,通过@Value("${属性名}")来加载配置文件中的属性值:

package com.spring.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @description
 * @author 子玉18164001916
 * @date 2020/7/1 9:04
 * @version
 */
@Component
public class TestConfig {

    @Value("${test.demo.name}")
    private String name;

    @Value("${test.demo.age}")
    private String age;

    public TestConfig() {
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

}

4 编写Controller,注入该Bean:

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestConfig testConfig;

          

/**
 * @description
 * @author 子玉18164001916
 * @date 2020/7/1 9:09
 * @version
 *
 */
@RequestMapping(value = "/getInfo")
String getInfo(){

    String info= "姓名:"+ testConfig.getName() + "\n年龄:"+testConfig.getAge();
    return info;
}

}

5.运行结果

 

6.在属性非常多的情况下,也可以定义一个和配置文件对应的Bean: 

package com.spring.demo.controller;

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

/**
 * @description:
 * @author: 子玉18164001916
 * @date: Created in 2020/7/1 9:16
 * @version: 3.0.0
 * @modified By:
 */
@ConfigurationProperties(prefix = "test.demo")
public class TestConfiguration {

    private String name;
    private String age;
    private String heignt;
    private String weight;
    private String info;



    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public TestConfiguration() {
    }

    public TestConfiguration(String name, String age, String heignt, String weight, String info) {
        this.name = name;
        this.age = age;
        this.heignt = heignt;
        this.weight = weight;
        this.info = info;
    }
    
    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getHeignt() {
        return heignt;
    }

    public void setHeignt(String heignt) {
        this.heignt = heignt;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }
}

7.通过注解@ConfigurationProperties(prefix="test.demo")指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名一一对应。

除此之外还需在Spring Boot入口类加上注解@EnableConfigurationProperties({TestConfiguration.class})来启用该配置:

@EnableConfigurationProperties({TestConfiguration.class})

 8.在application.properties配置文件中,各个属性可以相互引用,如下:

test.demo.name=zhangsan
test.demo.age=25
test.demo.heignt=188
test.demo.weight=150
test.demo.info=${test.demo.name}--${test.demo.name}--${test.demo.heignt}--${test.demo.weight}

 三、自定义配置文件

 1.定义一个对应该配置文件的Bean:

@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource("classpath:test.properties")
@Component
public class TestConfigBean {
    private String name;
    private int age;
    
}

 2.注解@PropertySource("classpath:test.properties")指明了使用哪个配置文件。要使用该配置Bean,同样也需要在入口类里使用注解@EnableConfigurationProperties({TestConfigBean.class})来启用该配置。

 四、通过命令行设置属性值

 

1.在运行Spring Boot jar文件时,可以使用命令java -jar xxx.jar --server.port=8081来改变端口的值。这条命令等价于我们手动到application.properties中修改(如果没有这条属性的话就添加)server.port属性的值为8081。

如果不想项目的配置被命令行修改,可以在入口文件的main方法中进行如下设置

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setAddCommandLineProperties(false);
    app.run(args);
}

 

 五、使用XML配置

 1.虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解@ImportResource({"classpath:some-application.xml"})来引入xml配置文件

 六、Profile配置

 1.Profile用来针对不同的环境下使用不同的配置文件,多环境配置文件必须以application-{profile}.properties的格式命,其中{profile}为环境标识。比如定义两个配置文件:

application-dev.properties:开发环境

server.port=8080

application-prod.properties:生产环境

server.port=8081

 

2.至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

如:spring.profiles.active=dev就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令java -jar xxx.jar --spring.profiles.active={profile}切换不同的环境配置。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值