Spring-创建对象的三种方式
spring创建对象的三种方式
1.通过无参构造函数
2.静态工厂(了解)
3.实例工厂(了解)
-----------------------------------------------------------------
准备:导包
------------------------------------------------------------------
通过无构造函数
applicationContext.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"> <bean id="p" class="com.Person"></bean> </beans>
Person.java
package com; public class Person { public void say() { System.out.println("hello world...."); } }
Test.java
package com; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { //方法一:无参构造函数 @Test public void test01() { //启动spring容器,读取src下applicationContext.xml文件 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //调用spring容器创建对象 Person p = (Person) ac.getBean("p"); p.say(); } }
静态工厂
applicationContext.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"> <bean id="f" class="com.factory.BeanFactory" factory-method="getS"></bean> </beans>
Persion.java
package com; public class Person { public void say() { System.out.println("hello world...."); } }
BeanFactory.java
package com.factory; import com.Person; public class BeanFactory { public static Person getInstance() { return new Person(); } }
Test.java
package com; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { //方法二:静态工厂创建对象 @Test public void test02() { //启动spring容器,读取src下的applicationContext.xml文件 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Person h = (Person) ac.getBean("f"); h.say(); } }
实例工厂(了解)
applicationContext.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"> <bean id="factory" class="com.factory.BeanFactory"></bean> <bean id="p" factory-bean="factory" factory-method="getInstance"></bean> </beans>
Person.java
package com; public class Person { public void say() { System.out.println("hello world...."); } }
BeanFactory.java
package com.factory; import com.Person; public class BeanFactory { public Person getInstance() { return new Person(); } }
Test.java
package com; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.factory.BeanFactory; public class Test{ //方法三:通过实例化工厂来创建对象 @Test public void test03() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); BeanFactory factory = (BeanFactory) ac.getBean("factory"); Person p = (Person)ac.getBean("p"); System.out.println(factory); System.out.println(p); } }
下一篇:spring--生命周期