Springboot框架实现了自动配置,在开发过程中,我们需要自定义一些配置可以用properties的形式进行配置,可以使用@Value注解来读取自定义配置。
application.properties中添加自定义配置
Person.java
@Component
public class Person {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private Integer age;
@Value("${person.sex}")
private String sex;
//略去getter、setter和toString方法
}
application.properties
person.age=25
person.name=lisan
person.sex=male
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyTest {
@Autowired
Person person;
@Test
public void jdbcTest() {
System.out.println(person);
}
}
结果

自定义配置文件添加配置
person.properties
person.age=25
person.name=lisan
person.sex=male
Person.java添加@PropertySource注解
@PropertySource(value = { "classpath:person.properties" }, ignoreResourceNotFound = true)
效果和上面一样