springboot版本:2.0.5.RELEASE
jdk:1.8
目标,根据一个properties文件获取内容,并添加到对象上。并通过springboot单元测试
在esources根目录下添加other.properties文件
other.properties内容
other.title=burns
other.blog=http://zhongyuele.top/
在com.esoon.ids.entity包中创建OtherProperties类
OtherProperties.java
package com.esoon.ids.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@Data
@ConfigurationProperties(prefix = "other")
@PropertySource(("classpath:other.properties"))
public class OtherProperties {
private String title;
private String blog;
}
添加测试类OtherPropertiesTest.java
package com.esoon.ids.entiry;
import com.esoon.ids.Ids20200121Application;
import com.esoon.ids.entity.OtherProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Ids20200121Application.class})
public class OtherPropertiesTest {
@Autowired
private OtherProperties op;
@Test
public void testOther() throws Exception{
System.out.println(op.toString());
}
}
输出
OtherProperties(title=burns, blog=http://zhongyuele.top/)
来源:CSDN
作者:爱的叹息
链接:https://blog.csdn.net/zp357252539/article/details/104068676