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

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

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

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

可以看到這個過程十分繁雜,但是在SpringBoot中這個過程將會變得非常簡單。

在SpringBoot中有如下3種方法:

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

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

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

代碼測試: properties配置文件:

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

Person類:

@Component ////只有在容器中的組件,才會擁有SpringBoot提供的強大功能
@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 + '\'' +
                '}';
    }
}

測試類如下:

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

測試結(jié)果:

@ConfigurationProperties注解+@EnableConfigurationProperties注解

這種方式@EnableConfigurationProperties注解一定要在配置類上寫,@EnableConfigurationProperties的意思是開啟屬性配置功能,開啟誰的屬性配置功能呢,因為我們上面的Person類想進行配置綁定,所以我們在后面加上參數(shù)Person.class:@EnableConfigurationProperties(Person.class)

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

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

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

@ConfigurationProperties注解+@Import注解

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

配置類: 

Person類: 

 測試類: 

 application.properties文件:

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

測試結(jié)果:

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

相關(guān)文章

最新評論

朝阳县| 大英县| 济宁市| 长子县| 左云县| 玛多县| 竹溪县| 清原| 峡江县| 大港区| 颍上县| 泸溪县| 西畴县| 卢龙县| 凤山市| 武平县| 承德县| 都兰县| 昂仁县| 普格县| 平罗县| 紫阳县| 互助| 昭平县| 珲春市| 崇文区| 泗洪县| 明溪县| 汉中市| 镶黄旗| 丹凤县| 高雄县| 石狮市| 会同县| 额敏县| 甘南县| 青铜峡市| 沅陵县| 巴中市| 博客| 通辽市|