最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法

 更新時(shí)間:2022年04月25日 16:53:29   作者:YuShiwen?  
這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法,文章內(nèi)容介紹詳細(xì)需要的小伙伴可以參考一下

properties配置文件如下:

human.name=Mr.Yu
human.age=21
human.gender=male

如何把properties里面的配置綁定到JavaBean里面,以前我們的做法如下:

public class PropertiesUtil {
    public static void getProperties(Person person) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/main/resources/demo.properties"));
        //得到配置文件key的名字
        Enumeration enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()){
            String key =(String)enumeration.nextElement();
            String value = properties.getProperty(key);
            System.out.println("key="+key+"——————value="+value);
            //封裝到JavaBean
            //以下內(nèi)容省略
        }
    }
}

輸出結(jié)果:

key=human.name——————value=Mr.Yu
key=human.age——————value=21
key=human.gender——————value=male

Process finished with exit code 0

可以看到這個(gè)過(guò)程十分繁雜,但是在SpringBoot中這個(gè)過(guò)程將會(huì)變得非常簡(jiǎn)單。

在SpringBoot中有如下3種方法:

1.@ConfigurationProperties注解+@Component(或@Controller或@Service或@Repository)注解

  • 只有在容器中的組件,才會(huì)擁有SpringBoot提供的強(qiáng)大功能,也就是如果我們需要使用到@ConfigurationProperties注解,那么我們首先要保證該對(duì)JavaBean對(duì)象在IoC容器中,所以需要用到Component注解來(lái)添加組件到容器中。
  • @ConfigurationProperties注解中prefix與value互相都是別名

  • 也就是說(shuō)@ConfigurationProperties(value = "human")@ConfigurationProperties(prefix = "human")是一樣的
  • prefix和value指的是前綴的意思

代碼測(cè)試: properties配置文件:

human.name=Mr.Yu
human.age=21
human.gender=male

Person類(lèi):

@Component ////只有在容器中的組件,才會(huì)擁有SpringBoot提供的強(qiáng)大功能
@ConfigurationProperties(value = "human")
public class Person {
    public String name;
    public int age;
    public String gender;
    public Person() {
    }
    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    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 getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

測(cè)試類(lèi)如下:

//@Controller
////@ResponseBody以字符串的方式寫(xiě)給瀏覽器,而不是跳轉(zhuǎn)到某個(gè)頁(yè)面
//@ResponseBody
//@RestController =  @Controller +  @ResponseBody
@RestController
public class HelloController {
    //自動(dòng)注入屬性
    @Autowired
    Person person;
    @RequestMapping("/person")
    public Person getPerson(){
        return person;
    }
}

測(cè)試結(jié)果:

@ConfigurationProperties注解+@EnableConfigurationProperties注解

這種方式@EnableConfigurationProperties注解一定要在配置類(lèi)上寫(xiě),@EnableConfigurationProperties的意思是開(kāi)啟屬性配置功能,開(kāi)啟誰(shuí)的屬性配置功能呢,因?yàn)槲覀兩厦娴腜erson類(lèi)想進(jìn)行配置綁定,所以我們?cè)诤竺婕由蠀?shù)Person.class:@EnableConfigurationProperties(Person.class)

@EnableConfigurationProperties(Person.class)的作用就是把這個(gè)Person組件注冊(cè)到容器中,對(duì)象的名稱(chēng)為:human-com.ysw.boot.properties.Person 其中human是@ConfigurationProperties(value = "human")中前綴value的值

在Person類(lèi)上還是有@ConfigurationProperties注解,這種方式把Person類(lèi)上的@Component注解換成了配置類(lèi)上的@EnableConfigurationProperties(Person.class)注解 

這種方式主要用在引用第三方包時(shí),比如第三方包中有一個(gè)Person類(lèi),該類(lèi)中沒(méi)有使用@Component,我們也不能夠給第三方包中的類(lèi)加上@Component,這個(gè)時(shí)候就可以使用在配置類(lèi)上加上@EnableConfigurationProperties注解的方法。

@ConfigurationProperties注解+@Import注解

  • 使用@Import給容器中自動(dòng)創(chuàng)建出這個(gè)類(lèi)型的組件、默認(rèn)組件的名字就是全類(lèi)名
  • @Import注解與2中的@ConfigurationProperties注解效果是一樣的
  • 只不過(guò)他們注冊(cè)到容器中的名稱(chēng)不同:
    • @ConfigurationProperties注解注冊(cè)到容器中對(duì)象的名稱(chēng)為:human-com.ysw.boot.properties.Person 其中human是@ConfigurationProperties(value = "human")中前綴value的值
    • @Import注解注冊(cè)到容器中對(duì)象的名稱(chēng)為:com.ysw.boot.properties.Person

配置類(lèi): 

Person類(lèi): 

 測(cè)試類(lèi): 

 application.properties文件:

server.port=8888
human.name=Mr.Yu
human.age=21
human.gender=male222

測(cè)試結(jié)果:

到此這篇關(guān)于SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法的文章就介紹到這了,更多相關(guān)SpringBoot配置綁定方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

嘉义县| 潼关县| 临安市| 荣昌县| 洛南县| 洛扎县| 平罗县| 巴塘县| 广安市| 湖北省| 甘南县| 平遥县| 轮台县| 哈巴河县| 房产| 洛川县| 巴塘县| 磐安县| 鲁甸县| 镇江市| 墨竹工卡县| 阿拉善盟| 柳河县| 永德县| 贵溪市| 勐海县| 衢州市| 略阳县| 武乡县| 长岛县| 米易县| 衡水市| 长泰县| 玉林市| 紫云| 平阳县| 封开县| 双辽市| 屏南县| 长顺县| 通海县|