Spring 依赖注入的几种方式
一、实体类-Student
package com.alipay.sofa.XX; import java.util.*; public class Student { private String name; private String[] books; private List<String> hobby; private Map<String,String> card; private Set<String> games; private Properties infos;
private Addresss addresss;
public String getName() { return name; } public void setName(String name) { this.name = name; } 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<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public Properties getInfos() { return infos; } public void setInfos(Properties infos) { this.infos = infos; }
public Addresss getAddresss() {
return addresss;
}
public void setAddresss(Addresss addresss) {
this.addresss = addresss;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", addresss=" + addresss.toString() +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
", infos=" + infos + '}';
}
}
二、实体类-Addresss
package com.alipay.sofa.XX; public class Addresss { private String addresss; public String getAddresss() { return addresss; } public void setAddresss(String addresss) { this.addresss = addresss; } @Override public String toString() { return "Addresss{" + "addresss='" + addresss + '\'' + '}'; } }
三、Bean文件
<?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="adresss" class="com.alipay.sofa.XX.Addresss">
<property name="addresss" value="西安" />
</bean>
<bean id="student" class="com.alipay.sofa.XX.Student">
<!-- 普通注入,用value--> <property name="name" value="设置的变量的值" />
<!-- Bean注入,用ref--> <property name="addresss" ref="addresss" />
<!-- 数组注入--> <property name="books"> <array> <value>红楼梦</value> <value>西游记</value> <value>三国演义</value> <value>水浒传</value> </array> </property>
<!-- list注入--> <property name="hobby"> <list> <value>听歌</value> <value>健身</value> <value>看书</value> <value>游泳</value> </list> </property>
<!-- Map注入--> <property name="card"> <map> <entry key="身份证" value="123...." /> <entry key="学生证" value="xx...." /> <entry key="银行卡" value="xx...." /> </map> </property>
<!-- Set注入--> <property name="games"> <set> <value>吃鸡</value> <value>王者</value> </set> </property>
<!-- Null值注入(其中空值,value属性为空即可:value="")--> <!-- <property name="someNull">--> <!-- <null/>--> <!-- </property>-->
<!-- property注入--> <property name="infos"> <props> <prop key="学号">20200908</prop> <prop key="姓名">小梅</prop> <prop key="性别">女</prop> </props> </property> </bean> </beans>
四、测试类
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { //CPX快捷键 ApplicationContext context = new ClassPathXmlApplicationContext(); Student student = context.getBean("student"); System.out.println(student.toString()); } }