springboot系列 | 配置文件

时间:2022-08-26 20:31:58

可采用该网站https://start.spring.io/进行springboot项目创建

生成pom.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.cwh</groupId>
<artifactId>springbootConfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springbootConfig</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
以下都使用该pom.xml文件

一、自定义属性

当我们新建一个springboot项目时该目录/src/main/resources下会有一个application.properties配置文件,我们自定义属性在该文件进行自定义,添加以下两个属性:

my.name: cwh
my.age: 25
然后进行读取配置文件的属性值:

package com.cwh.springbootConfig.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@Value("${my.name}")
private String name;
@Value("${my.age}")
private int age;

@RequestMapping(value = "/hello")
public String hello(){
return "hello:"+name+":"+age;
}

}
运行以下代码即可:

package com.cwh.springbootConfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootConfigApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootConfigApplication.class, args);
}
}
通过浏览器访问http://localhost:8080/hello:

hello:cwh:25


二、配置文件属性值注入实体类

配置文件添加如下内容:

my.name: cwh
my.age: 25
my.number: ${random.int}
my.uuid ${random.uuid}
my.max: ${random.int(10)}
my.value: ${random.value}
my.greeting: hi,i'm ${my.name}
实体类:

package com.cwh.springbootConfig.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@ConfigurationProperties(prefix = "my")
//@Component
public class User {

private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}


}
注意: "@ConfigurationProperties"主动加载resource下的application.properties文件

新建controller:

package com.cwh.springbootConfig.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cwh.springbootConfig.model.User;

@RestController
@EnableConfigurationProperties({User.class})
public class UserController {

@Autowired
User user;

@RequestMapping(value = "/user")
public String user(){
return user.getGreeting()+" >>>>"+user.getName()+" >>>>"+ user.getUuid()+" >>>>"+user.getMax();
}
}
注意:@EnableConfigurationProperties({User.class})如果有该注解那么User里不需要@Component这个注解,如果User里写有@Component,该注解可以不需要


运行浏览器访问http://localhost:8080/user:

hi,i'm cwh >>>>cwh >>>>b86fa46c-b294-474e-8df9-a7c1848a61a2 >>>>8

三、自定义配置文件
新建自定义配置文件test.properties:

hello.name: menco
hello.age: 25
hello.number: ${random.int}
hello.uuid ${random.uuid}
hello.max: ${random.int(10)}
hello.value: ${random.value}
hello.greeting: hi,i'm ${hello.name}
实体类:

package com.cwh.springbootConfig.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "hello")


//@Component
public class User {

private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}


}
注意:"@ConfigurationProperties"主动加载resource下的application.properties文件,如果自定义一个其他名配置文件可以加上以上两个注解@Configuration和@PropertySource,需要注意一点是:自定义的test.properties如果也是和application.properties的prefix一样的话,那么他还是会去自动加载application.properties文件

运行访问http://localhost:8080/user:

hi,i'm menco >>>>menco >>>>bd054df6-5565-48a5-b99e-c3611481c910 >>>>1

四、多配置文件切换

新建application-dev.properties,命名规则application-***.properties:

my.name: dev
my.age: 25
my.number: ${random.int}
my.uuid ${random.uuid}
my.max: ${random.int(10)}
my.value: ${random.value}
my.greeting: hi,i'm ${my.name}
在application.properties添加如下内容:

##use application-dev.properties
spring.profiles.active: dev
注意:dev对应上面配置文件的命名规则***

实体类:

package com.cwh.springbootConfig.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@ConfigurationProperties(prefix = "my")

//@Component
public class User {

private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}


}

运行访问http://localhost:8080/user:

hi,i'm dev >>>>dev >>>>bd054df6-5565-48a5-b99e-c3611481c910 >>>>1

源码下载地址: https://github.com/Menc0/springbootConfig