1.直接创建SpringBoot项目(web项目)
2.我们最开始进入,自动导入的是@org.junit.jupiter.api.Test注解包,我们先改成@org.junit.Test下的Test注解类
3. 我们在添加注解之后发现,并没有可运行的方法按钮,这是因为类前面需要加上修饰符public才会出现运行按钮
4. 加上public之后,运行方法的箭头便出现了
5.注解@SpringBootTest(classes = Application.class),小编在测试获取yml文件数据的时候,发现抛了一个异常
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javafx.application.Application]: Is it an abstract class?; nested exception is java.lang.InstantiationException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:208) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
... 41 common frames omitted
Caused by: java.lang.InstantiationException: null
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[na:1.8.0_45]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_45]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
... 43 common frames omitted
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) ~[spring-test-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) ~[spring-test-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.2.6.RELEASE.jar:5.2.6.RELEASE]
注意:这里把@SpringBootTest括号中的注释删除即可
5.1 Person类
@Component
@ConfigurationProperties(prefix = "person") //添加之后在yml文件中有提示
public class Person {
private String name;
private Integer age;
private Boolean boos;
private Date date;
private Map<String ,Object> maps;
private List<Object> lists;
private Dog dog;
...( set() .. get() )
}
5.1.1 注配置文件坐标:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
5.2 yml文件
最终输出:
person:Person{name='zhangsan',
age=18, boos=false,
date=Mon May 11 00:00:00 CST 2020,
maps={k1=v1, k2=v2},
lists=[lisi, zhangsan, liumi, meimei],
dog=Dog{name='小狗', age=23}}
这里也是平常没注意的地方,小编被懵了很久,也希望能够帮助大家少走点弯路