前言
在上一篇文章中,我们创建了一个简单的SpringBoot程序,做了初步的了解。这篇文章中,我们将对SpringBoot的配置文件application.properties做个分析。
正文
在SpringBoot中,使用了一个全局的配置文件application.properties,放在src/main.resources目录下或者类路径的/config下,它的作用是对默认的配置进行修改。
com.xiaojian.name=123啊8
然后在使用的地方用注解@Value(value="${config.name}")
就可以绑定在属性上:
@Value("${com.xiaojian.name}")
private String name;
@RequestMapping("/")
public String index() {
return name;
}
启动项目,在地址栏输入http://localhost:8080就可以看到打印出了常量的值。
当属性太多的时候,我们可以直接将属性绑定在一个bean上,顶部使用注解@ConfigurationProperties(prefix="com.xiaojian")
来指明绑定的属性
package com.xiaojian.demo1;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@ConfigurationProperties(prefix = "com.xiaojian")
public class ConfigBean {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
配置完bean后,还需要在springboot的入口类上添加注解@EnableConfigurationProperties
指明加载那个bean.最后在controller中直接注入就可以使用get/set方法来取到定义的值。
package com.xiaojian.demo1;
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.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Demo1Application {
@Autowired
private ConfigBean configBean;
@RequestMapping("/")
public String index() {
return configBean.getName() + configBean.getAge();
}
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
在application.properties中的各个参数之间也可以直接引用,如下:
com.xiaojian.name=123啊8
com.xiaojian.age = 啦啦啦啦
com.xiaojian.count = ${com.xiaojian.name}和${com.xiaojian.age}
这样我们就可以使用com.xiaojian.count
来取到定义name和age。
@ConfigurationProperties(prefix = "com.xiaojian")
@Configuration
@PropertySource("classpath:test.properties")
public class ConfigBean {
private String name;
private int age;
需要注意的是,如果是1.5以上的版本的springboot,可以直接在配置bean上使用@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")
来指定文件位置,1.5版本以后,就需要使用@Configuration
和@PropertySource(“classpath:test.properties”)
一起才行,并且,如果你使用的是IDEA,会提示一个警告如下:
要解决这个警告,就要在pom.xml文件中加入以下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
xiaojian.secret=${random.value}
xiaojian.number=${random.int}
xiaojian.bignumber=${random.long}
xiaojian.uuid=${random.uuid}
xiaojian.number.less.than.ten=${random.int(10)}
xiaojian.number.in.range=${random.int[1024,65536]}
SpringBoot项目是基于jar包运行的,所以可以直接用命令行来启动:
java -jar xxx.jar
也可以在命令行启动时修改tomcat的端口号:
java -jar xxx.jar --server.port=8081
在命令行中,--
代表对application.properties中的属性进行赋值.
SpringBoot可以从多种书属性源获得属性值:
- 根目录下的开发工具全局设置属性(当开发工具激活时为~/.spring-boot-devtools.properties)。
- 测试中的@TestPropertySource注解。
- 测试中的@SpringBootTest#properties注解特性。
- 命令行参数
- SPRING_APPLICATION_JSON中的属性(环境变量或系统属性中的内联JSON嵌入)。
- ServletConfig初始化参数。
- ServletContext初始化参数。
- java:comp/env里的JNDI属性
- JVM系统属性
- 操作系统环境变量
- 随机生成的带random.* 前缀的属性(在设置其他属性时,可以应用他们,比如${random.long})
- 应用程序以外的application.properties或者appliaction.yml文件
- 打包在应用程序内的application.properties或者appliaction.yml文件
- 通过@PropertySource标注的属性源
- 默认属性(通过SpringApplication.setDefaultProperties指定).
以上列表的属性,如果有重名的,上面的会覆盖下面的,优先级一次递减。
- 相对于应用程序运行目录的、config子目录
- 应用程序运行的目录
- config包内
- Classpath根目录
覆盖关系从上而下,上面目录中的会覆盖下面的:src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性
,且此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml。
application-dev.properties 开发环境:server.port=9001
application-prod.properties 生产环境:server.port=9002
想要使用对应的环境,只需要在application.properties中使用spring.profiles.active属性来设置,值对应上面提到的{profile},这里就是指dev、prod这2个。
当然你也可以用命令行启动的时候带上参数:
java -jar xxx.jar --spring.profiles.active=dev
这样就可以使用不用的配置文件.
也可以通过在application.properties文件中指定属性spring.profiles.active
来确定使用哪个配置文件.
除了可以用profile来区分不同的配置文件,在代码里面,我们可以直接使用@Profile
注解来就行配置,如下的数据库配置:
首先定义一个接口:
package com.xiaojian.demo1;
public interface DBConnector {
public void configure();
}
可以定义两个实现类:
@Component
@Profile("testdb")
public class TestDBConnector implements DBConnector {
@Override
public void configure() {
System.out.println("testdb");
}
}
@Component
@Profile("devdb")
public class DevDBconnector implements DBConnector {
@Override
public void configure() {
System.out.println("devdb");
}
}
在配置文件中,我们就可以激活对应的类来使用:spring.profiles.active=testdb
在使用的时候:
@Autowired
DBConnector dbConnector;
@RequestMapping("/")
public String index() {
dbConnector.configure();
return "";
}
除了使用spring.profiles.active来激活不同的profile,我们也可以使用spring.profiles.include来引入多个profile
spring.profiles.active: testdb
spring.profiles.include: proddb,prodmq
总结
这里只对springboot中一部分配置做说明,除了这些,springboot还提供了很多的配置项,可以去下面的链接查看:springboot常用属性汇总