SpringBoot不仅仅只支持@value的形式读取配置,还能够同过spring管理的bean上添加@ConfigurationProperties来获取。接下来咱们看看如何使用@ConfigurationProperties读取配置值的。html
在application.properties文件中添加如下配置,固然在application-dev.properties文件中添加也是能够的。java
#应用id spring.application.id=spring-boot-wusy-demo #应用名称 spring.application.name=spring-boot-wusy-demo #编码设置 server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.force=true spring.http.encoding.enabled=true spring.messages.encoding=UTF-8 # tomcat 配置 server.tomcat.accept-count=1000 server.tomcat.max-threads=500 # session超时时间,单位是秒 server.servlet.session.timeout=1800 #当前应用http端口 server.port=8787 #访问地址,该配置能够不配置,则直接经过ip+port访问 #server.servlet.context-path=/demo spring.profiles.active = dev
建立一个SpringApplicationProperties类,来获取spring.application开头的配置git
import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; /** * @author wusy * Company: xxxxxx科技有限公司 * Createtime : 2020/2/25 22:57 * Description : 读取spring.application开头的配置值 */ @Configuration @ConfigurationProperties(prefix = "spring.application") @Setter @Getter public class SpringApplicationProperties { /** * 应用id */ private String id ; /** * 应用名称 */ private String name; }
这里引入了lombok开发辅助包,有关idea安装lombok包的插件能够参考https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html这里就很少说了。这里要主要这个类要添加@Configuration注解,让spring在扫描的时候会扫描到该类,这里须要提到的一点是SpringBoot的默认扫描路径是启动类所在的package,固然也可经过在启动类中添加@ComponentScan来进行其余路径的扫描。web
引入pom.xml中引入spring-boot-configuration-processor包spring
<?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.wusy.demo</groupId> <artifactId>spring-boot-wusy-demo</artifactId> <version>1.0-SNAPSHOT</version> <!-- 继承SpringBoot父包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath></relativePath> </parent> <dependencies> <!-- 引入lombok,方便开发 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- spring boot web支持:mvc,aop... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
最后经过@Autowired自动注入SpringApplicationProperties类,来使用配置信息apache
import com.wusy.demo.config.SpringApplicationProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @author wusy * Company: xxxxxx科技有限公司 * Createtime : 2020/2/24 21:54 * Description : */ @RestController @RequestMapping("/api/demo") public class HelloWorldController { @Autowired private SpringApplicationProperties springApplicationProperties; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "hello world," + springApplicationProperties.getName(); } }
打开浏览器,在地址栏输入http://127.0.0.1:8787/api/demo/helloapi
至此,SpringBoot经过@ConfigurationProperties来获取配置信息例子演示完毕。浏览器
项目码云地址:https://gitee.com/wusycode/spring-boot-wusy-demo.gittomcat