欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈Spring Boot 属性配置和自定义属性配置

程序员文章站 2023-08-19 15:55:00
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个starte...

在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个starter都有默认配置,而这些默认配置足以满足正常的功能开发。

如果需要修改自定义修改默认配置,spring boot 提供了很简便的方法,只需要在application.properties 中添加修改相应的配置。(spring boot启动的时候会读取application.properties这份默认配置)

一、修改默认配置

例1、spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录:

server.port=8888

重启项目,启动日志可以看到:tomcat started on port(s): 8888 (http) 启动端口为8888,浏览器中访问 http://localhost:8888 能正常访问。

例2、spring boot 开发中的数据库连接信息配置(这里使用com.alibaba 的 druid), 在application.properties 添加以下记录:

druid.url=jdbc:mysql://192.168.0.20:3306/test
druid.driver-class=com.mysql.jdbc.driver
druid.username=root
druid.password=123456
druid.initial-size=1
druid.min-idle=1
druid.max-active=20
druid.test-on-borrow=true

以上两个例子,说明了如需修改starter模块中的默认配置,只需要在在application.properties 添加需要修改的配置即可。

附: application.properties 全部配置项,点击查看spring boot 所有配置说明

二、自定义属性配置

在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性,并在实体bean中加载出来。

1、在application.properties中添加自定义属性配置

com.sam.name=sam
com.sam.age=11
com.sam.desc=magical sam

2、编写bean类,加载属性

sam类需要添加@component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。

第一种:使用spring支持的@value()加载

package com.sam.demo.conf;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;

/**
 * @author sam
 * @since 2017/7/15
 */
@component
public class sam {
  //获取application.properties的属性
  @value("${com.sam.name}")
  private string name;

  @value("${com.sam.age}")
  private int age;

  @value("${com.sam.desc}")
  private string desc;
  
  //getter & setter
}

第二种:使用@configurationproperties(prefix="") 设置前缀,属性上不需要添加注解。

package com.sam.demo.conf;
import org.springframework.stereotype.component;
/**
 * @author sam
 * @since 2017/7/15
 */
@component
@configurationproperties(prefix = "com.sam")
public class sam {
  private string name;
  private int age;
  private string desc;
  //getter & setter
}

3、在controller中注入并使用sam这个bean。

package com.sam.demo.controller;
import com.sam.demo.conf.sam;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
 * @author sam
 * @since 2017/7/14
 */
@restcontroller
public class indexcontroller {
  @autowired
  private sam sam;
  @requestmapping("/index")
  public string index() {
    system.out.println(sam.getname() + " " + sam.getage() + " " + sam.getdesc());
    return "index";
  }
}

浏览器访问:http://localhost:8080/index ,控制台正常打印出sam的内容。

三、application.properties 属性配置详解

1、参数引用与random随机数方法的使用

在application.properties内可以直接通过${}引用其他属性的值,如下:

com.sam.name=sam
com.sam.age=11
com.sam.desc=${name} is ${age} years old. 

在application.properties中如果需要获取随机数,可以通过${random},如下:

#获取随机字符串
com.sam.randomvalue=${random.value}

#获取随机字符串:${random.value}
#获取随机int:${random.int}
#获取10以内的随机数:${random.int(10)}
#获取10-20的随机数:${random.int[10,20]}
#获取随机long:${random.long}
#获取随机uuid:${random.uuid}

2、多环境配置

实际开发中可能会有不同的环境,有开发环境、测试环境、生成环境。对于每个环境相关配置都可能有所不同,如:数据库信息、端口配置、本地路径配置等。

如果每次切换不同环境都需要修改application.properties,那么操作是十分繁琐的。在spring boot中提供了多环境配置,使得我们切换环境变得简便。

在application.properties同目录下新建一下三个文件:

application-dev.properties   //开发环境的配置文件
application-test.properties   //测试环境的配置文件
application-prod.properties   //生产环境的配置文件

上面三个文件分别对应了 开发、测试、生产 的配置内容,接下来就是应该怎么选择性引用这些配置了。

在application.properties添加:

spring.profiles.active=dev
#引用测试的配置文件
#spring.profiles.active=test
#引用生产的配置文件
#spring.profiles.active=prod

添加spring.profiles.active=dev后启动应用,会发现引用了dev的这份配置信息。

可以看出上面三个配置文件符合 application-{profile}.properties 格式,而在application.properties添加的spring.profiles.active=dev 中的dev正是上面配置文件中的 profile。根据具体环境进行切换即刻。

用命令运行jar包启动应用的时候,可以指定相应的配置.

java -jar demo-0.0.1-snapshot.jar --spring.profiles.active=dev

附:配置方式和优先级
这些方式优先级如下:
a. 命令行参数
b. 来自java:comp/env的jndi属性
c. java系统属性(system.getproperties())
d. 操作系统环境变量
e. randomvaluepropertysource配置的random.*属性值
f. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
g. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
h. jar外部的application.properties或application.yml(不带spring.profile)配置文件
i. jar内部的application.properties或application.yml(不带spring.profile)配置文件
j. @configuration注解类上的@propertysource
k. 通过springapplication.setdefaultproperties指定的默认属性

注:命令行参数这种jar包指定参数启动应用的方式,可能是不安全的,我们可以设置禁止这种方式启动应用,如下:

springapplication.setaddcommandlineproperties(false);

package com.sam.demo;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
@springbootapplication
public class demoapplication {

  public static void main(string[] args) {
//    springapplication.run(demoapplication.class, args);
    springapplication springapplication = new springapplication(demoapplication.class);
    //禁止命令行设置参数
    springapplication.setaddcommandlineproperties(false);
    springapplication.run(args);
  }
}

补充:

在spring boot 中配置除了支持 application.properties,还支持application.yml的配置方式,如下:

新建application.yml代替application.properties

server:
 port: 9999

com:
 sam:
  name: sam
  age: 11
  desc: magical sam

注意:port: 9999 中间是有空格的,yml语法请参考:yml配置文件用法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。