用spring boot获取自定义配置的值
value方法
//Bookcontroller类
package com.yan.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class BookController {
@Value("${book.author}")
private String author;
@Value("${book.name}")
private String name;
@RequestMapping("/bookInfo")
@ResponseBody
public String showInfo() {
return author+":"+name;
}
public static void main(String[] args) {
SpringApplication.run(BookController.class, args);
}
}
//application.properties
book.author=Tom
book.name=SpringBoot
//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.yan</groupId>
<artifactId>Springboot-Demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
代码中用到的内容
配置文件中
spring-boot-starter-
- 先继承spring boot父类
- 因为要用到web所以继承web
- 设置JRE版本
自定义的配置文件
- 名称=值
Java文件中 - 设置自动配置
- 获取自定义值内容
@Value("${book.author}")
//获取到的book.author的值传给下面设置的private类的author
private String author;
@Value("${book.name}")
private String name;
- 写一个返回获取到内容值的方法
@RequestMapping("/bookInfo")
@ResponseBody//因为要直接获取值
- 写main方法,设置入口
类型安全方法
package com.yan.controller;
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;
@Controller
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "book")
public class BookController {
//@Value("${author}")
private String author;
//@Value("${name}")
private String name;
@RequestMapping("/bookInfo")
@ResponseBody
public String showInfo() {
return author+":"+name;
}
public static void main(String[] args) {
SpringApplication.run(BookController.class, args);
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
book.author=Tom2
book.name=SpringBoot
pom.xml与value方法的相同
类型安全方法的注意事项
- @ConfigurationProperties(prefix = “book”)
prefix后面写的是变量的一个共同特征(book.author book.name) - private String author; 这个定义的类名需要与自定义的 . 之后的相同,会赋值给该类名
- 这个方法中的每个成员变量都需要有get、set方法
快速补充get、set方法
value与类型安全方法的不同之处
- value中需要对每个自定义内容写一行value代码,对于自定义变量多的不友好
- 类型安全方法只需要加一句@ConfigurationProperties(prefix = “book”)即可