DI依赖注入
环境搭建
pojo类
Student
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
private Set<String> games;
private String wife;
private Properties 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> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
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;
}
}
Address
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
实现
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
完善注入信息
<?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.Google.pojo.Address">
<property name="address" value="江西"/>
</bean>
<bean id="student" class="com.Google.pojo.Student">
<!--一般属性注入-->
<property name="name" value="Spring"/>
<!--Bean注入-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>三国演义</value>
<value>水湖庄</value>
<value>西游记</value>
<value>红楼梦</value>
</array>
</property>
<!--集合-->
<property name="hobbys">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
<!--Map-->
<property name="cards">
<map>
<entry key="学生卡" value="23123123"></entry>
<entry key="好人卡" value="123123123"></entry>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>王者荣耀</value>
<value>CSGO</value>
</set>
</property>
<!--null-->
<property name="wife" value=""/>
<!--Properties-->
<property name="info">
<props>
<prop key="Driver">com.Google</prop>
<prop key="ROOT">root</prop>
<prop key="Password">123123</prop>
</props>
</property>
</bean>
</beans>
结果
Student{
name='Spring',
address=com.Google.pojo.Address@2ea41516,
books=[三国演义, 水湖庄, 西游记, 红楼梦],
hobbys=[唱, 跳, rap, 篮球],
cards={学生卡=23123123, 好人卡=123123123},
games=[LOL, 王者荣耀, CSGO],
wife='',
info={ROOT=root, Driver=com.Google, Password=123123}
}