Spring依賴注入多種類型數(shù)據(jù)的示例代碼
Student實(shí)體類
package entity;
import java.util.*;
/**
* @author LeDao
* @company
* @create 2022-02-13 21:26
*/
public class Student {
private int id;
private String name;
private StudentClass studentClass;
private String[] books;
private List<String> hobbies;
private Map<String, String> cards;
private Set<String> games;
private String wife;
private Properties info;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public StudentClass getStudentClass() {
return studentClass;
public void setStudentClass(StudentClass studentClass) {
this.studentClass = studentClass;
public String[] getBooks() {
return books;
public void setBooks(String[] books) {
this.books = books;
public List<String> getHobbies() {
return hobbies;
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
public Map<String, String> getCards() {
return cards;
public void setCards(Map<String, String> cards) {
this.cards = cards;
public Set<String> getGames() {
return games;
public void setGames(Set<String> games) {
this.games = games;
public String getWife() {
return wife;
public void setWife(String wife) {
this.wife = wife;
public Properties getInfo() {
return info;
public void setInfo(Properties info) {
this.info = info;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", studentClass=" + studentClass +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
StudentsClass實(shí)體類
package entity;
/**
* @author LeDao
* @company
* @create 2022-02-14 14:11
*/
public class StudentClass {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Class{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
beans.xml
下面展示的數(shù)據(jù)類型有:一般類型、對象、數(shù)組、List、Map、Set、空值、Properties
<?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="studentClass1" class="entity.StudentClass">
<property name="id" value="1"/>
<property name="name" value="軟件工程3班"/>
</bean>
<bean id="user1" class="entity.Student">
<!--一般類型-->
<property name="id" value="1"/>
<property name="name" value="tom"/>
<!--對象-->
<property name="studentClass" ref="studentClass1"/>
<!--數(shù)組-->
<property name="books">
<array>
<value>Java編程思想</value>
<value>MySQL必知必會</value>
<value>平凡的世界</value>
</array>
</property>
<!--List-->
<property name="hobbies">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>打籃球</value>
</list>
</property>
<!--Map-->
<property name="cards">
<map>
<entry key="身份證" value="123"/>
<entry key="校園卡" value="321"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>DNF</value>
<value>COC</value>
</set>
</property>
<!--空值-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="userName">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
測試
import config.MyConfig;
import entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author LeDao
* @company
* @create 2022-02-12 15:56
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("user1");
System.out.println(student);
}
}
到此這篇關(guān)于Spring依賴注入多種類型數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Spring依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot項(xiàng)目基于Devtools實(shí)現(xiàn)熱部署步驟詳解
這篇文章主要介紹了Springboot項(xiàng)目基于Devtools實(shí)現(xiàn)熱部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
mybatis于xml方式和注解方式實(shí)現(xiàn)多表查詢的操作方法
在數(shù)據(jù)庫中,單表的操作是最簡單的,但是在實(shí)際業(yè)務(wù)中最少也有十幾張表,并且表與表之間常常相互間聯(lián)系,本文給大家介紹mybatis于xml方式和注解方式實(shí)現(xiàn)多表查詢的操作方法,感興趣的朋友一起看看吧2023-12-12
Java實(shí)現(xiàn)解析第三方接口返回的json
在實(shí)際開發(fā)過程中,免不了和其他公司進(jìn)行聯(lián)調(diào),調(diào)用第三方接口,這個(gè)時(shí)候我們就需要根據(jù)對方返回的數(shù)據(jù)進(jìn)行解析,獲得我們想要的字段,下面我們就來看看具體有哪些方法吧2024-01-01
SpringBoot整合JDBC的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot整合JDBC的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
詳解Java的Hibernate框架中的List映射表與Bag映射
這篇文章主要介紹了Java的Hibernate框架中的List映射表與Bag映射,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12

