欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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);
    }
}

輸出結果:

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'}}

注:如果顯示亂碼,設置文件編碼為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);
    }
}

運行結果:

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

其他

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

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

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

相關文章

  • 基于eclipse-temurin鏡像部署spring boot應用的實現(xiàn)示例

    基于eclipse-temurin鏡像部署spring boot應用的實現(xiàn)示例

    本文提供了基于eclipse-temurin鏡像部署Spring Boot應用的詳細實現(xiàn)示例,通過使用Docker鏡像,可以輕松地創(chuàng)建和管理Spring Boot應用程序的容器化環(huán)境,感興趣的可以了解一下
    2023-08-08
  • Java,JSP,Servlet獲取當前工程路徑(絕對路徑)問題解析

    Java,JSP,Servlet獲取當前工程路徑(絕對路徑)問題解析

    這篇文章主要介紹了Java,JSP,Servlet獲取當前工程路徑(絕對路徑)問題解析,需要的朋友可以參考下。
    2017-09-09
  • JavaWeb實現(xiàn)文件上傳與下載實例詳解

    JavaWeb實現(xiàn)文件上傳與下載實例詳解

    在Web應用程序開發(fā)中,文件上傳與下載功能是非常常用的功能,下面通過本文給大家介紹JavaWeb實現(xiàn)文件上傳與下載實例詳解,對javaweb文件上傳下載相關知識感興趣的朋友一起學習吧
    2016-02-02
  • spring @EventListener 事件與監(jiān)聽的示例詳解

    spring @EventListener 事件與監(jiān)聽的示例詳解

    本文介紹了自定義Spring事件和監(jiān)聽器的方法,包括如何發(fā)布事件、監(jiān)聽事件以及如何處理異步事件,通過示例代碼和日志,展示了事件的順序執(zhí)行和異步處理機制,感興趣的朋友一起看看吧
    2025-03-03
  • Java如何調用Matlab程序

    Java如何調用Matlab程序

    這篇文章主要介紹了Java如何調用Matlab程序的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot之@Scheduled注解用法解讀

    SpringBoot之@Scheduled注解用法解讀

    這篇文章主要介紹了SpringBoot之@Scheduled注解用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 深入解讀Java代碼組織中的package包結構

    深入解讀Java代碼組織中的package包結構

    這篇文章主要介紹了Java代碼組織中的package包結構,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2016-03-03
  • 如何利用SpringBoot搭建WebService服務接口

    如何利用SpringBoot搭建WebService服務接口

    之前項目經(jīng)理想要開發(fā)一個webservice的協(xié)議,給我一個星期的時間,后面用springboot開發(fā)了webservice,這篇文章主要給大家介紹了關于如何利用SpringBoot搭建WebService服務接口的相關資料,需要的朋友可以參考下
    2023-11-11
  • SpringBoot中的@ApiModelProperty注解作用

    SpringBoot中的@ApiModelProperty注解作用

    這篇文章主要介紹了SpringBoot中的@ApiModelProperty注解作用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • SpringBoot2.x 集成 Thymeleaf的詳細教程

    SpringBoot2.x 集成 Thymeleaf的詳細教程

    本文主要對SpringBoot2.x集成Thymeleaf及其常用語法進行簡單總結,其中SpringBoot使用的2.4.5版本。對SpringBoot2.x 集成 Thymeleaf知識感興趣的朋友跟隨小編一起看看吧
    2021-07-07

最新評論