本文主要是介绍记一次@ConfigurationProperties的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 实体类
package com.example.whatisspringboot.annotation;/*** @Auther: 洺润Star* @Date: 2019/10/9 15:53* @Description:*/
public class Person{/*** 知识点:* 1.元注解是可以注解在其它注解上的注解* 2.组合注解是被注解的注解* 3.@Value注解*/private String name;private int age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "Persion{" +"name='" + name + '\'' +", age=" + age +", sex='" + sex + '\'' +'}';}
}
- 配置文件如下
person.name=MRStar
person.age=18
person.sex=male
- 启动类如下
package com.example.whatisspringboot;import com.example.whatisspringboot.annotation.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;@SpringBootApplication
public class WhatIsSpringBootApplication {//在此对应配置文件@Bean@ConfigurationProperties(prefix = "person")public Person person(){return new Person();}public static void main(String[] args) {SpringApplication.run(WhatIsSpringBootApplication.class, args);}
}
- 测试
package com.example.whatisspringboot;import com.example.whatisspringboot.annotation.Person;
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
public class WhatIsSpringBootApplicationTests {@AutowiredPerson person;@Testpublic void contextLoads() {System.out.println(person.toString());//Persion{name='MRStar', age=18, sex='male'}}}
这篇关于记一次@ConfigurationProperties的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!