`
java1996
  • 浏览: 5237 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
社区版块
存档分类
最新评论

三中实例化bean的方式(学习笔记)

阅读更多
bean配置:
     <!-- 三种实例化bean的配置 -->
      <!-- 第一种 :默认构造方法-->
      <bean id="personService" class="com.river.service.impl.PersonServiceBean"/>
      <!-- 第二种 :静态工厂方法-->
      <bean id="personService1" class="com.river.service.impl.PersonServiceBeanFactory" factory-method="personService"></bean>
      <!-- 第三种 :实例工厂方法-->
      <bean id="personServiceFactory" class="com.river.service.impl.PersonServiceBeanFactory"></bean>
      <bean id="personService2" factory-bean="personServiceFactory" factory-method="personService1"></bean>

工厂类:
  /**
* 工厂方法实例化bean
* @author river
*
*/
public class PersonServiceBeanFactory
{
public static PersonService personService()
{
return new PersonServiceBean();
}
public  PersonService personService1()
{
return new PersonServiceBean();
}
}

PersonServiceBean类:
    public class PersonServiceBean implements PersonService {

/* (non-Javadoc)
* @see com.river.spring.PwesionService#save()
*/
public void save()
{
System.out.println("save ()");
}
}

测试:
   public void instanceSpring()
{
//RiverClassPathXMLApplicationContext ctx = new RiverClassPathXMLApplicationContext("beans.xml");
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService2");
ps.save();
}

结果:save()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics