/*没有IOC*/
Student student = new Student();
/* 使用IOC,studnet对象可以从spring容器中获取*/
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1 = ac.getBean("student1", Student.class);
在applicationContext.xml文件中可以对对象进行配置,这些对象由spring进行统一管理。以Student类为例,其对象创建有3中主要方式
public class Student {
private int id;
private String name;
private int age;
private int tid;
private String[] array;
private Set<String> set;
private List<String> list;
private Map<String, String> map;
private Teacher teacher;
public Student() {
}
public Student(int id, String name, int age, int tid) {
this.id = id;
this.name = name;
this.age = age;
this.tid = tid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
<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="student1" class="com.bear.sxt.pojo.Student"/>
<bean id="student2" class="com.bear.sxt.pojo.Student">
<constructor-arg index="0" name="id" type="int" value="1"/>
<constructor-arg index="1" name="name" type="java.lang.String" value="有参构造方法"/>
<constructor-arg index="2" name="age" type="int" value="25"/>
<constructor-arg index="3" name="tid" type="int" value="1"/>
bean>
beans>
public class StudentFactory {
public Student newInstance() {
return new Student(1, "实例工厂", 25, 1);
}
}
<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="factory" class="com.bear.sxt.factory.StudentFactory"/>
<bean id="student3" factory-bean="factory" factory-method="newInstance"/>
beans>
public class StaticStudentFactory {
public static Student newInstance() {
return new Student(2, "静态工厂", 25, 2);
}
}
<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="student4" class="com.bear.sxt.factory.StaticStudentFactory" factory-method="newInstance"/>
beans>
<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="student5" class="com.bear.sxt.pojo.Student">
<property name="id" value="3"/>
<property name="name">
<value>属性赋值测试value>
property>
<property name="age">
<value>25value>
property>
<property name="tid" value="2"/>
<property name="array">
<array>
<value>1value>
<value>2value>
<value>3value>
array>
property>
<property name="set">
<set>
<value>1value>
<value>2value>
<value>3value>
set>
property>
<property name="list">
<list>
<value>1value>
<value>2value>
<value>3value>
list>
property>
<property name="map">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
map>
property>
<property name="teacher" ref="teacher"/>
bean>
<bean id="teacher" class="com.bear.sxt.pojo.Teacher">
<property name="id" value="1"/>
<property name="name" value="DI测试"/>
bean>
beans>
加载applicationContext.xml文件后,对象就已经创建了并由spring容器管理。无论你使不使用这些对象,这些对象都是存在的。
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过applicationContext.xml文件中配置的bean的id号获取对应的bean对象
Student student1 = ac.getBean("student1", Student.class);
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
String[] names = ac.getBeanDefinitionNames();
for (String s : names) {
System.out.println(s);
}
用于控制对象的有效范围.