Spring boot 基础配置

前言

上一篇文章介绍了如何开启spring boot,这篇文章将介绍spring boot的一些基本配置,通过这篇文章,你将知道:spring boot全局配置、配置文件的优先级、如何读取自定义配置文件、如何通过命令行方式运行、如何定制Banner等

定制Banner

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

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

只需要在src/main/resources目录下新建banner.txt文件,把我们自己的图案复制进去,就能自定义这个图案,当然可以通过网站:http://patorjk.com/software/taag/ 一键生成,我们使用Aurora生成,效果如下

配置文件的优先级

application.propertiesapplication.yml文件可以放在以下四个位置:

  • 外置,在相对于应用程序运行目录的/congfig子目录里。
  • 外置,在应用程序运行的目录里
  • 内置,在config包内
  • 内置,在Classpath根目录

如果你在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml

测试:在resources目录下分别新建application.properties和application.yml,指定端口启动

application.properties内容

server.port=8081

application.yml内容

server:
  port: 8080

启动服务,可以看到spring boot加载的是application.properties的内容

自定义属性的读取

有时候项目需要用到自定义的属性,在application.properties中定义

扫描二维码关注公众号,回复: 3620166 查看本文章
me.zhengjie.name=zhengjie
me.zhengjie.pass=123456

使用@Value(value=”${属性名称}”),将值绑定到相应属性上,并且在启动类中加入控制器,使其可以直接访问,如:

@RestController
@SpringBootApplication
public class SpringbootDeployApplication {

    @Value("${me.zhengjie.name}")
    private String name;

    @Value("${me.zhengjie.pass}")
    private String pass;

    @GetMapping("/")
    public String index(){
        return "name:"+name+",pass:"+pass;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDeployApplication.class, args);
    }
}

启动项目,访问http://localhost:8081,页面显示如下:

在属性非常多的情况下,也可以定义一个和配置文件对应的Bean:
通过注解@ConfigurationProperties(prefix=“me.zhengjie”)指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名对应。

@Component
@ConfigurationProperties(prefix="me.zhengjie")
public class MyProperties {

    private String name;

    private String pass;

    // get,set略
}

在之前的启动类中,注入该Bean,即可获取属性值

	@Autowired
    private MyProperties myProperties;

    @GetMapping("/test")
    public String index1(){
        return "name:"+myProperties.getName()+",pass:"+myProperties.getPass();
    }

启动项目,访问http://localhost:8081/test,页面显示如下

属性间的引用

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

me.zhengjie.name=zhengjie
me.zhengjie.pass=123456

me.zhengjie.nameAndPass=${me.zhengjie.name}--${me.zhengjie.pass}

自定义配置文件

在src/main/resources路径下新建一个application-test.properties配置文件

me.zhengjie.test=test

定义一个对应的Bean

@Configuration
@ConfigurationProperties(prefix="me.zhengjie")
@PropertySource("classpath:application-test.properties")
@Component
public class MyTestProperties {

    private String test;

    // get,set略
}

注解@PropertySource(“classpath:application-test.properties”)指明了使用哪个配置文件

命令行方式运行

通过命令行的方式运行spring boot程序

java -jar xx.jar     --使用默认的配置文件
运行外部配置文件
java -Dspring.config.location=E:\resources\xx.properties -jar xx.jar
修改端口号
java -jar xx.jar --server.port=8888

项目源码

github:https://github.com/dqjdda/SpringBoot_All

码云:https://gitee.com/hgpt/SpringBoot_All

开源后台管理系统:

欢迎体验 Aurora

github: https://github.com/dqjdda/Aurora

码云: https://gitee.com/hgpt/Aurora

猜你喜欢

转载自blog.csdn.net/zj7321/article/details/82973684
今日推荐