您好,登錄后才能下訂單哦!
這篇文章主要介紹“Spring依賴注入多種類型數據的代碼怎么寫”,在日常操作中,相信很多人在Spring依賴注入多種類型數據的代碼怎么寫問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Spring依賴注入多種類型數據的代碼怎么寫”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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 + '}'; }
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 + '\'' + '}'; } }
下面展示的數據類型有:一般類型、對象、數組、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"/> <!--數組--> <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); } }
到此,關于“Spring依賴注入多種類型數據的代碼怎么寫”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。