Spring Boot一些基础配置

定制Banner

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

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

我们可以把这个图案修改为自己想要的。在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。


 _______           _______  _______ _________
(  ____ \|\     /|(  ____ \(  ____ \\__   __/
| (    \/| )   ( || (    \/| (    \/   ) (  
| (_____ | | _ | || (__    | (__       | |   
(_____  )| |( )| ||  __)   |  __)      | |  
      ) || || || || (      | (         | |  
/\____) || () () || (____/\| (____/\   | |  
\_______)(_______)(_______/(_______/   )_(   
                                             
2020-05-29 17:20:11.525  INFO 3620 --- [

全局配置文件

在src/main/resources目录下,Spring Boot提供了一个名为application.properties的全局配置文件,可对一些默认配置的配置值进行修改。
application.properties中可配置所有官方属性
Spring Boot允许我们在application.properties下自定义一些属性,比如:

test.demo.name=zhangsan
test.demo.age=22

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

@RestController
public class TestController {
    @Value("${test.demo.name}")
    public String name;
    @Value("${test.demo.age}")
    public int age;

    @RequestMapping("/")
    String getHelloWord() {
        return "名字:"+name+"年龄:"+age;
    }
}

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

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

@Component
@ConfigurationProperties(prefix="test.demo")
public class Demo {
    public String name;
    public int age;
    //需要get  set 方法 此处为了长度省略了
}
@SpringBootApplication
@EnableConfigurationProperties({Demo.class})
public class StartSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBootApplication.class, args);
    }
}

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

test.demo.name=zhangsan
test.demo.age=22
test.demo.student=${test.demo.name}--${test.demo.age}

自定义配置文件

除了可以在application.properties里配置属性,我们还可以自定义一个配置文件。在src/main/resources目录下新建一个test.properties:

test.demo.name=zhangsan
test.demo.age=22
定义一个对应该配置文件的Bean:

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

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

使用xml配置

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

Profile配置

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

application-dev.properties:开发环境

server.port=8080

application-prod.properties:生产环境

server.port=8081

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

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

参考文献: https://mrbird.cc/Spring-Boot%20basic%20config.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值