部门类
package com.xu.entity;
public class Depart {
private String dname;
private String dvalue;
@Override
public String toString() {
return "Depart{" +
"dname='" + dname + '\'' +
", dvalue='" + dvalue + '\'' +
'}';
}
public Depart() {
}
public Depart(String dname) {
this.dname = dname;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDvalue() {
return dvalue;
}
public void setDvalue(String dvalue) {
this.dvalue = dvalue;
}
public Depart(String dname, String dvalue) {
this.dname = dname;
this.dvalue = dvalue;
}
}
用户类
package com.xu.entity;
import java.util.*;
public class User {
private String name;
private Depart depart;
private String[] books;
private List<String> hobbys;
private Map<String, String> card;
private Set<String> games;
private Properties info;
public User() {
}
public User(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student:" +
"name='" + name + '\'' + "\n" +
" address=" + depart.toString() + "\n" +
" books=" + Arrays.toString(books) + "\n" +
" hobbys=" + hobbys + "\n" +
" card=" + card + "\n" +
" games=" + games + "\n" +
" info=" + info + "\n";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Depart getDepart() {
return depart;
}
public void setDepart(Depart depart) {
this.depart = depart;
}
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 Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
}
静态工厂类
package com.xu.utils;
import com.xu.entity.Depart;
public class DepartFactory {
public static Depart createdepart(){
Depart de=new Depart();
de.setDname("研发部门");
de.setDvalue("666666");
return de;
}
}
配置文件
<?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">
<!-- 1.通过set注入-->
<bean id="user" class="com.xu.entity.User">
<property name="name" value="徐总"></property>
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>学习</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="123456"/>
<entry key="银行卡" value="123456"/>
</map>
</property>
<property name="games">
<set>
<value>王者荣耀</value>
<value>贪吃蛇</value>
</set>
</property>
<property name="info">
<props>
<prop key="学号">123456789</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
<property name="depart" ref="departs">
</property>
</bean>
<bean id="departs" class="com.xu.entity.Depart">
<property name="dname" value="研发部门"></property>
<property name="dvalue" value="20211104"></property>
</bean>
<!-- 2.通过构造方法注入-->
<bean id="user2" class="com.xu.entity.User">
<constructor-arg index="0" type="java.lang.String" value="小徐"></constructor-arg>
</bean>
<!-- 3.通过静态工厂注入-->
<bean id="factory" class="com.xu.utils.DepartFactory" factory-method="createdepart"></bean>
<bean id="user3" class="com.xu.entity.User">
<property name="depart" ref="factory"></property>
</bean>
<!-- bean里面的属性值-->
</beans>
测试类
import com.xu.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUserDepart {
@Test
public void test(){
ClassPathXmlApplicationContext contest = new ClassPathXmlApplicationContext("userdepart.xml");
User user = (User) contest.getBean("user");
System.out.println(user.toString());
}
@Test
public void test2(){
ClassPathXmlApplicationContext contest = new ClassPathXmlApplicationContext("userdepart.xml");
User user = (User) contest.getBean("user2");
System.out.println(user.getName());
}
@Test
public void test3(){
ClassPathXmlApplicationContext contest = new ClassPathXmlApplicationContext("userdepart.xml");
User user = (User) contest.getBean("user3");
System.out.println(user.getDepart());
}
}
在pom.xml文件中配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
总结:
1.通过set注入
2.通过构造方法注入
3.通过静态工厂注入