三种实例化bean的方式
1.使用类构造器实例化
<bean id=“orderService" class="cn.itcast.OrderServiceBean"/>
2.使用静态工厂方法实例化
<bean id="persionServiceBean2" class="cn.com.xinli.service.impl.PersionServiceBeanFactory" factory-method="createPersionServiceBean"/>
public class PersionServiceBeanFactory
{
public static PersionServiceBean createPersionServiceBean()
{
return new PersionServiceBean();
}
}
例子:
(1).首先写工厂类.他其中包含产生我们的业务bean的方法
- package cn.com.xinli.service.impl;
-
-
- public class PersionServiceBeanFactory
- {
- public static PersionServiceBean createPersionServiceBean()
- {
- return new PersionServiceBean();
- }
- }
package cn.com.xinli.service.impl;
public class PersionServiceBeanFactory
{
public static PersionServiceBean createPersionServiceBean()
{
return new PersionServiceBean();
}
}
(2).改写beans.xml :包含工厂类类名和产生业务bean的方法名字
<bean id="persionServiceBean2" class="cn.com.xinli.service.impl.PersionServiceBeanFactory" factory-method="createPersionServiceBean"/>
(3) 测试
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean2");
-
- ps.save();
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean2");
ps.save();
(4) 结果
2009-05-24 14:34:00,781 INFO (PersionServiceBean.java:12) - 我是save()方法!
3.使用实例工厂方法实例化:
<bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
<bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
public PersionServiceBean createPersionServiceBean2()
{
return new PersionServiceBean();
}
例子:
(1). 首先写工厂类.他其中包含产生我们的业务bean的方法 ,在已有代码的基础上
- package cn.com.xinli.service.impl;
-
-
- public class PersionServiceBeanFactory
- {
- public static PersionServiceBean createPersionServiceBean()
- {
- return new PersionServiceBean();
- }
-
- <SPAN style="COLOR: #ff0000">public PersionServiceBean createPersionServiceBean2()
- {
- return new PersionServiceBean();
- }</SPAN>
-
- }
package cn.com.xinli.service.impl;
public class PersionServiceBeanFactory
{
public static PersionServiceBean createPersionServiceBean()
{
return new PersionServiceBean();
}
public PersionServiceBean createPersionServiceBean2()
{
return new PersionServiceBean();
}
}
(2).改写beans.xml :写两个bean,一个是工厂bean,一个是利用工厂bean产生业务bean的bean.
- <bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
- <bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
<bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
<bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
(3) 测试
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean3");
-
- ps.save();
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean3");
ps.save();
(4) 结果
2009-05-24 14:49:17,812 INFO (PersionServiceBean.java:12) - 我是save()方法!
(5) 注意,其实方式2和方式3的区别就在 工厂类中是如何产生业务bean的,方式2是static方式,方式3不是