1.使用类构造器实例化(调用默认构造函数来完成实例化)
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
2.使用静态工厂方法实例化
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBeanFactory" factory-method = "createPersonServiceBean"/>
这里的class是工厂类,factory-method指定的是静态工厂方法,就是说通过工厂类PersonServiceBeanFactory的createPersonServiceBean方法创建personService这个bean实例。是什么bean呢?是由创建方法的返回类型决定
public class PersonServiceBeanFactory{ public static PersonServiceBean createPersonServiceBean(){ return new PersonServiceBean(); } }
3.使用实例工厂方法实例化:
<bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"/> <bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"/>
public class PersonServiceBeanFactory { public PersonServiceBean createPersonServiceBean2(){ return new PersonServiceBean(); } }
这方法跟第2的静态工厂方法不同之处是,创建方法没有static关键字。这时候必须要先实例化工厂
CC霜是什么
PersonServiceBeanFactory, 通过这代码实例化:,
实例化了工厂之后呢,再使用工厂bean personServiceFactory调用它里面这个专门用于创建bean的工厂方法,来创建bean
Spring
实例化bean就有这三种实现方式,具体使用哪一种可以根据自己的需求来定,当然绝大部分,应该说90%多使用第一种实例化方案,
代码如下:
PersonService.java
package cn.itcast.service; public interface PersonService { public void save(); } PersonServiceBean.java package cn.itcast.service.impl; import cn.itcast.service.PersonService; public class PersonServiceBean implements PersonService { public void save(){ System.out.println("我是save()方法"); } }
PersonServiceBeanFactory.java
package cn.itcast.service.impl; public class PersonServiceBeanFactory { //静态工厂方法创建bean public static PersonServiceBean createPersonServiceBean(){ return new PersonServiceBean(); } //使用实例工厂方法创建bean, 这里没有了static关键字 public PersonServiceBean createPersonServiceBean2(){ return new PersonServiceBean(); } }
SpringTest.java
package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.service.PersonService; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); //PersonService personService = (PersonService)ctx.getBean("personService1"); //PersonService personService = (PersonService)ctx.getBean("personService2"); PersonService personService = (PersonService)ctx.getBean("personService3"); personService.save(); } }
beans.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-2.5.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> <!-- 使用静态工厂方法创建bean --> <bean id="personService2" class="cn.itcast.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/> <!-- 使用实例工厂方法创建bean --> <bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"/> <!--将工厂类交给Spring管理,--> <bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"/> </beans>