SpringBoot获取全局配置文件的属性以及@ConfigurationProperties实现类型安全的配置

在SpringBoot,可以定义一个全局配置文件,全局配置文件有两种形式:
1、 application.properties
2、application.yml

二者的后缀名不同,编辑的格式也不同,但都是全局配置文件,二者选其一即可,都可以起到相同的作用

在你的maven工程下的src/main/resources 新建一个文件,以 application.properties为例。

然后编辑以下内容如下:
在这里插入图片描述
在这里插入图片描述
这里配置了 server:
port: 8082
等等访问地址为:http://localhost:8082/hello

然后在src/main/java下新建一个book.class并写入以下的代码
在这里插入图片描述
在这里插入图片描述

package test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@EnableAutoConfiguration //自动配置 ?
@Controller

public class book {
	
	@Value("${book.name}")
	private String name;
	@Value("${book.author}")
	private String author;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	@RequestMapping("hello")
	@ResponseBody
	public String helloTest(){
		System.out.println(name+""+author);
		return"hello"+"    "+name+"    "+author;
	}
   public static void main(String[] args) {
	SpringApplication.run(book.class, args);
}
}


保存后构建maven,然后运行该book.class

就可以在相应的地址(http://localhost:8082/hello)获取对应的属性。
运行结果

在这里插入图片描述

如果application.properties中的属性比较少,用上述的方式是可行的,但是如果application.properties中有很多的属性的时候,每次都要写一次@Value比较麻烦,

于是有 @ConfigurationProperties实现类型安全的配置(在application.properties的基础上)。

如下图:@ConfigurationProperties(prefix = “book”),写在获取成员变量的类前。

application.properties配置文件的内容和上面的相同,具体的实现如下:
在这里插入图片描述


package test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@EnableAutoConfiguration //自动配置 ?
@Controller
@ConfigurationProperties(prefix = "book")
public class book {
	
	//@Value("${book.name}")
	private String name;
	//@Value("${book.author}")
	private String author;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	@RequestMapping("hello-info")
	@ResponseBody
	public String helloTest(){
		System.out.println(name+""+author);
		return"helloInfo"+"    "+name+"    "+author;
	}
   public static void main(String[] args) {
	SpringApplication.run(book.class, args);
}
}

运行结果:

在这里插入图片描述

对于类中的成员变量命名,要是application.properties属性中除了前缀外的剩余部分,比如我的application.properties中的 book.name,那么想获取其属性时,就要定义一个String name,此外,成员变量需要get和set方法。

          最后,关于application.properties配置文件中,属性的命名最好都已相同的前缀开头(比如上面都是以book开头)

这样,就不用每次都写@Value了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章