背景:为了更好的测试,你就得更好地了解开发,为了更好的了解开发,你就知道开发常用框架,那就来吧,第一个springboot
目的:使用注解读取application.yml配置文件中信息
组网图:不涉及
工具:java version “1.8.0_65” ;Apache Maven 3.6.3;IDEA版本 2018.3 (准备步骤见本人其它博文)
步骤:
使用@ConfigurationProperties注解读取application.yml文件,如下
server:
port: 8080
servlet:
context-path: /v1
woman:
age: 25
girl: 那个我喜欢的人
attribute: beautiful
添加WomanConfig类
package com.hua.myfirst;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
@ConfigurationProperties(prefix = "woman")
public class WomanConfig {
private BigDecimal age;
private String girl;
private String attribute;
public BigDecimal getAge() {
return age;
}
public void setAge(BigDecimal age) {
this.age = age;
}
public String getGirl() {
return girl;
}
public void setGirl(String girl) {
this.girl = girl;
}
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
}
使用@ConfigurationProperties报错:Spring Boot Configuration Annotation Processor not found in claspath
解决方法参考此博客:https://blog.youkuaiyun.com/li1325169021/article/details/91984562
更改HelloConteoller类
package com.hua.myfirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloConteoller {
@Autowired
private WomanConfig womanConfig;
@GetMapping("/hello")
public String hello() {
return "She is " + womanConfig.getAttribute() + "girl";
}
}
启动springboot
打开链接:http://127.0.0.1:8080/v1/hello
备注: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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hua</groupId>
<artifactId>myfirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myfirst</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
启动类如下:
package com.hua.myfirst;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyfirstApplication {
public static void main(String[] args) {
SpringApplication.run(MyfirstApplication.class, args);
}
}