写实体类且需要有构造方法、Set、Get、toString方法;
package com.bean;
public class Student {
private int sid;
private String sname;
public Student() {
}
public Student(int sid, String sname) {
this.sid = sid;
this.sname = sname;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
'}';
}
}
package com.bean;
import java.util.*;
public class Teacher {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public Teacher() {
}
public Teacher(String[] myStrs, List<String> myList, Set<String> mySet, Map<String, String> myMap, Properties myProps) {
this.myStrs = myStrs;
this.myList = myList;
this.mySet = mySet;
this.myMap = myMap;
this.myProps = myProps;
}
public String[] getMyStrs() {
return myStrs;
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public Set<String> getMySet() {
return mySet;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public Map<String, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public String toString() {
return "Teacher{" +
"myStrs=" + Arrays.toString(myStrs) +
", myList=" + myList +
", mySet=" + mySet +
", myMap=" + myMap +
", myProps=" + myProps +
'}';
}
}
写Spring配置文件,创建对象并赋值;
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--常用的创建对象的方法-->
<bean id="dao101" class="com.dao.DemoDaoImpl"/>
<bean id="service101" class="com.service.DemoServiceImpl"/>
<bean id="student101" class="com.bean.Student">
<!--常用依赖注入,其实就是给类赋值-->
<property name="sid" value="1"/>
<property name="sname" value="xiaona"/>
</bean>
<bean id="stu2" class="com.bean.Student">
<!--构造函数注入-->
<constructor-arg name="sid" value="2"/>
<constructor-arg name="sname" value="2ya"/>
</bean>
<bean id="teacher101" class="com.bean.Teacher">
<!--复杂数据类型-->
<property name="myStrs">
<array>
<value>str1</value>
<value>str2</value>
<value>str3</value>
</array>
</property>
<property name="myList">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="mySet">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="101" value="map01"/>
<entry key="102" value="map02"/>
<entry key="103" value="map03"/>
</map>
</property>
<property name="myProps">
<props>
<prop key="101">pro01</prop>
<prop key="102">pro02</prop>
<prop key="103">pro03</prop>
</props>
</property>
</bean>
</beans>
测试输出
import com.bean.Student;
import com.bean.Teacher;
import com.dao.DemoDaoImpl;
import com.service.DemoServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest101 {
@Test
public void testSpring(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
DemoDaoImpl dao = (DemoDaoImpl) context.getBean("dao101");
DemoServiceImpl service = (DemoServiceImpl) context.getBean("service101");
Student student = (Student) context.getBean("student101");
Student student2 = (Student) context.getBean("stu2");
Teacher teacher = (Teacher) context.getBean("teacher101");
System.out.println(student.toString());
System.out.println(student2.toString());
System.out.println(teacher.toString());
}
}
所需依赖
<dependencies>
<!--IOC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--可以自动导入-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>