Spring DI依赖注入(maven项目)
1、spring maven 项目环境的创建请参考我的这篇博客。
2、项目结构如下:
3、创建实体类
创建com.sw.entity package包,在entity包下新建Address和Student实体类。
Address.java
package com.sw.entity;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Address{" +
"name='" + name + '\'' +
'}';
}
}
Student.java
package com.sw.entity;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
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> 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 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{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
4、创建spring容器
在resource目录下创建beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.sw.entity.Address">
<property name="name" value="狗蛋儿"/>
</bean>
<bean name="student" class="com.sw.entity.Student">
<!--普通值注入 value-->
<property name="name" value="xiaowu"/>
<!--bean注入 ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books" >
<array>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<!--list注入-->
<property name="hobbys" >
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="1234567890"/>
<entry key="银行卡" value="0987654321"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>AOA</value>
<value>BOB</value>
<value>COC</value>
<value>LOL</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="driver">musql</prop>
<prop key="rul">www.baidu.com</prop>
<prop key="username">root</prop>
<prop key="pwd">123456</prop>
</props>
</property>
</bean>
</beans>
5、创建测试类
DiTest.java
import com.sw.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DiTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student.toString());
}
}
输出结果:
Student{
name=‘xiaowu’,
address=Address{name=‘狗蛋儿’},
books=[西游记, 三国演义, 水浒传, 红楼梦],
hobbys=[唱, 跳, rap, 篮球],
card={身份证=1234567890,
银行卡=0987654321},
games=[AOA, BOB, COC, LOL],
wife=‘null’,
info={driver=musql, pwd=123456, username=root, rul=www.baidu.com}
}