可采用该网站https://start.spring.io/进行springboot项目创建
生成pom.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>以下都使用该pom.xml文件
<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>
一、自定义属性
当我们新建一个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;通过浏览器访问http://localhost:8080/hello:
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);
}
}
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;注意: "@ConfigurationProperties"主动加载resource下的application.properties文件
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;
}
}
新建controller:
package com.cwh.springbootConfig.controller;注意:@EnableConfigurationProperties({User.class})如果有该注解那么User里不需要@Component这个注解,如果User里写有@Component,该注解可以不需要
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();
}
}
运行浏览器访问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;注意:"@ConfigurationProperties"主动加载resource下的application.properties文件,如果自定义一个其他名配置文件可以加上以上两个注解@Configuration和@PropertySource,需要注意一点是:自定义的test.properties如果也是和application.properties的prefix一样的话,那么他还是会去自动加载application.properties文件
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;
}
}
运行访问http://localhost:8080/user:
hi,i'm menco >>>>menco >>>>bd054df6-5565-48a5-b99e-c3611481c910 >>>>1
四、多配置文件切换
新建application-dev.properties,命名规则application-***.properties:
my.name: dev在application.properties添加如下内容:
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}
##use application-dev.properties注意:dev对应上面配置文件的命名规则***
spring.profiles.active: 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