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

SpringBoot給類進行賦初值的四種方式

 更新時間:2024年08月08日 09:56:55   作者:岳軒子  
這篇文章主要介紹了springboot給類進行賦初值的四種方式,并通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

1. 使用@Value和@ConfigurationProperties

這里不加贅述了,前面我也發(fā)過,這里就放個鏈接吧
@Value獲取值和@ConfigurationProperties獲取值用法及比較(springboot)

2. 使用@PropertySource

創(chuàng)建Person.java

package com.example.springbootdaily2.model;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@PropertySource(value = "classpath:person.properties")
// 這個是前綴的意思
@ConfigurationProperties(prefix = "person2")
public class PersonX {
    private String name;
    private Character sex;
    @DateTimeFormat(pattern = "YYYY-MM-SS")
    private Date birthday;
    private Integer age;
    private String address;
    private Map<String, Integer> maps;
    private List<String> lists;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Character getSex() {
        return sex;
    }

    public void setSex(Character sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Map<String, Integer> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Integer> maps) {
        this.maps = maps;
    }

    public List<String> getLists() {
        return lists;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

創(chuàng)建person.properties

person2.name="李四"
person2.sex=男
person2.birthday=2022-02-07
person2.age=18
person2.maps.keys1=16
person2.maps.keys2=16
person2.lists=[12,24,57]
person2.address="保定廉恥"
person2.dog.name=${random.value}

寫一個測試類

package com.example.springbootdaily;
import com.example.springbootdaily.model.Dog;
import com.example.springbootdaily.model.Person;
import com.example.springbootdaily.model.Person2;
import com.example.springbootdaily.model.PersonX;
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 SpringTest {
    @Autowired
    PersonX personX;

    @Test
    public void print4(){
        System.out.println(personX);
    }
}

輸出結(jié)果:

Person{name='"岳軒子"', sex=M, 
birthday=Sun Dec 26 00:00:00 CST 2021, age=18, 
address='"保定武漢"', maps={keys2=16, keys1=16}, lists=[[12, 24, 57]], 
dog=Dog{name='cdab390f55c9f8a6bbb420cd15607add'}}

注:如果顯示亂碼,設(shè)置文件編碼為utf-8

3. 使用@ImportResource

Student類

package com.example.springbootdaily.model;

public class Student {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

創(chuàng)建beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.example.springbootdaily.model.Student">
        <property name="name" value="李四"/>
        <property name="age" value="18"/>
    </bean>
</beans>

在主類中引入

package com.example.springbootdaily;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations = "classpath:beans.xml")
public class SpringbootDailyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDailyApplication.class, args);
    }

}

測試

package com.example.springbootdaily;


import com.example.springbootdaily.model.*;
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 SpringTest {
    @Autowired
    Student student;

    @Test
    public void print5(){
        System.out.println(student);
    }
}

運行結(jié)果:

Student{name='李四', age=18}

其他

我們可以導(dǎo)入配置文件處理器,以后編寫配置就有提示了
<!‐‐導(dǎo)入配置文件處理器,配置文件進行綁定就會有提示‐‐>
依賴:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring‐boot‐configuration‐processor</artifactId>
     <optional>true</optional>
</dependency>

以上就是SpringBoot給類進行賦初值的四種方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot給類進行賦初值的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java實現(xiàn)讀取和寫入properties文件

    Java實現(xiàn)讀取和寫入properties文件

    這篇文章主要介紹了Java實現(xiàn)讀取和寫入properties文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • java軟引用在瀏覽器使用實例講解

    java軟引用在瀏覽器使用實例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于java軟引用在瀏覽器使用實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-04-04
  • MyBatis-Plus使用動態(tài)表名分表查詢的實現(xiàn)

    MyBatis-Plus使用動態(tài)表名分表查詢的實現(xiàn)

    本文主要介紹了MyBatis-Plus使用動態(tài)表名分表查詢,主要是動態(tài)修改表名的幾種常見場景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-11-11
  • 深入理解final變量的初始化

    深入理解final變量的初始化

    本篇文章是對final變量的初始化進行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 教你使用eclipse?搭建Swt?環(huán)境的全過程

    教你使用eclipse?搭建Swt?環(huán)境的全過程

    本文給大家分享使用eclipse?搭建Swt?環(huán)境的全過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • Java跨域問題的處理詳解

    Java跨域問題的處理詳解

    這篇文章主要給大家介紹了關(guān)于Java跨域問題處理的相關(guān)資料,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • java版十大排序經(jīng)典算法:完整代碼(4)

    java版十大排序經(jīng)典算法:完整代碼(4)

    優(yōu)秀的文章也不少,但是Java完整版的好像不多,我把所有的寫一遍鞏固下,同時也真誠的希望閱讀到這篇文章的小伙伴們可以自己去從頭敲一遍,不要粘貼復(fù)制!希望我的文章對你有所幫助,每天進步一點點
    2021-07-07
  • 解決java 命令行亂碼的問題

    解決java 命令行亂碼的問題

    這篇文章主要介紹了解決java 命令行亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Mybatis自定義SQL的關(guān)系映射、分頁、排序功能的實現(xiàn)

    Mybatis自定義SQL的關(guān)系映射、分頁、排序功能的實現(xiàn)

    這篇文章主要介紹了Mybatis自定義SQL的關(guān)系映射、分頁、排序功能的實現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 教你怎么通過IDEA設(shè)置堆內(nèi)存空間

    教你怎么通過IDEA設(shè)置堆內(nèi)存空間

    這篇文章主要介紹了教你怎么通過IDEA設(shè)置堆內(nèi)存空間,文中有非常詳細(xì)的代碼示例,對正在使用IDEA的小伙伴們很有幫助喲,需要的朋友可以參考下
    2021-05-05

最新評論

卓尼县| 洪江市| 台北县| 南宫市| 额敏县| 达日县| 澄江县| 淮安市| 黄龙县| 永泰县| 车险| 清流县| 南平市| 登封市| 西乌珠穆沁旗| 客服| 乌审旗| 罗田县| 军事| 大石桥市| 益阳市| 楚雄市| 平凉市| 页游| 荃湾区| 金坛市| 荔浦县| 柞水县| 灌南县| 延川县| 南岸区| 兴宁市| 金门县| 清苑县| 澄江县| 衡阳县| 浮梁县| 星子县| 祥云县| 安溪县| 临潭县|