Spring入門學習---02
阿新 • • 發佈:2021-11-03
Spring
1、Spring注入的具體例項
建立兩個實體類
1、Address.class
package com.charles.pojo; public class Address { private String address; public Address(){} public Address(String address) { this.address = address; } public String getAddress() { return address; } publicvoid setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
2、Custom.class
package com.charles.pojo; import java.util.*; public classCustom { private String name; private Address address; private String[] books; private List<String> hobby; private Map<Integer,String> idCard; private Set<String> games; private String wife; private Properties info; public Custom(){} publicCustom(String name, Address address, String[] books, List<String> hobby, Map<Integer, String> idCard, Set<String> games, String wife, Properties info) { this.name = name; this.address = address; this.books = books; this.hobby = hobby; this.idCard = idCard; this.games = games; this.wife = wife; this.info = info; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Map<Integer, String> getIdCard() { return idCard; } public void setIdCard(Map<Integer, String> idCard) { this.idCard = idCard; } 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 "Custom{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobby=" + hobby + ", idCard=" + idCard + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
3、ApplicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="address" class="com.charles.pojo.Address"> <property name="address" value="ShangHai"/> </bean> <bean id="custom" class="com.charles.pojo.Custom"> <property name="name" value="Charles"/> <property name="address" ref="address"/> <property name="books"> <array> <value>MYSQL</value> <value>DOCKER</value> <value>JAVA</value> </array> </property> <property name="hobby"> <list> <value>GAMING</value> <value>CODING</value> <value>DANCING</value> </list> </property> <property name="idCard"> <map> <entry key="1" value="441521"/> <entry key="2" value="20021116"/> </map> </property> <property name="games"> <set> <value>GTA</value> <value>APEX</value> <value>LOL</value> </set> </property> <property name="wife"> <null/> </property> <property name="info"> <props> <prop key="email">[email protected]</prop> <prop key="username">Charles_H</prop> <prop key="phone">110</prop> </props> </property> </bean> </beans>
4、進行測試
@Test public void test2(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(context.getBean("custom").toString()); }
測試結果
2、IOC建立物件的方式
1、無參構造建立物件(預設方式)
2、有參構造:
1. 下標賦值
<constructor-arg index="0" value="Charles"/>
2. 型別賦值
<constructor-arg type="java.lang.String" value="Charles"/>
3. 直接通過引數名來賦值
<constructor-arg name="id" value="1"/> <constructor-arg name="username" value="Hello~"/>
3、關於Spring的配置
別名
<alias name="custom" alias="cs"/> <bean id="cs" class="com.charles.pojo.Custom"/>
import 用於合併配置檔案
由於在實際開發當中,我們需要配置的東西非常多。因此,為了不弄混,我們的配置都是分開進行配置,最後在進行合併,最後只有呼叫該配置即可。
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="beans.xml"/> </beans>