application.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置类名,用于反向创建一个person对象
同时给一个编号方便查找-->
<bean id="date1" class="java.util.Date"/>
<bean id="person" class="com.lp.domain.Person"/>
<bean id="person2" class="com.lp.domain.Person">
<property name="id" value="10"/>
<property name="name" value="rose"/>
<property name="age" value="20"/>
<property name="date" ref="date1"/>
</bean>
<bean id="person3" class="com.lp.domain.Person">
<constructor-arg name="id" value="10"/>
<constructor-arg name="name" value="tony"/>
<constructor-arg name="age" value="20"/>
<constructor-arg name="date" ref="date1"/>
</bean>
<!-- 静态工厂 factory-method="getBean" getBean()为静态方法 -->
<bean id="person4" factory-method="getBean" class="com.lp.domain.PersonFactory"/>
<!-- PersonFactory2 factory2 = new PersonFactory2(); 创建工厂
Person person3= factory2.getBean(); 调用工厂方法-->
<bean class="com.lp.domain.PersonFactory2" id="factory2"/>
<!-- 实例工厂 factory-bean="factory2" factory-method="getBean" getBean()为成员方法-->
<bean factory-bean="factory2" factory-method="getBean" id="person5"/>
</beans>
Test01SpringIOC
package com.lp.demo01;
import com.lp.domain.Person;
import com.lp.domain.PersonFactory2;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01SpringIoc {
@Test
public void test01(){
//1:创建容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2:给定配置文件名称 applicationContext.xml
//3:调用容器的getBean方法获取id对应的对象
Person person = (Person) context.getBean("person");
Person person2 = context.getBean("person",Person.class);
System.out.println(person);
System.out.println(person2);
}
@Test
public void test02(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person2 = context.getBean("person2",Person.class);
System.out.println(person2);
}
@Test
public void test03(){
//1:创建容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2:给定配置文件名称 applicationContext.xml
//3:调用容器的getBean方法获取id对应的对象
Person person3= context.getBean("person3",Person.class);
System.out.println(person3);
}
@Test
public void test04(){
//1:创建容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2:给定配置文件名称 applicationContext.xml
//3:调用容器的getBean方法获取id对应的对象
Person person4= context.getBean("person4",Person.class);
System.out.println(person4);
}
@Test
public void test05(){
//1:创建容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2:给定配置文件名称 applicationContext.xml
//PersonFactory2 factory2 = new PersonFactory2();
//Person person3= factory2.getBean();
Person person3 = context.getBean("person5",Person.class);
System.out.println(person3);
}
}
PersonFactory
package com.lp.domain;
public class PersonFactory {
private static Person getBean(){
return new Person();
}
}
PersonFactory2
package com.lp.domain;
public class PersonFactory2 {
public Person getBean(){
return new Person();
}
}
1017

被折叠的 条评论
为什么被折叠?



