spring<1>----bean实例化
PersonService.java
package cn.anycall.service;public interface PersonService {public abstract void save();}
package cn.anycall.service.impl;import cn.anycall.service.PersonService;public class PersonServiceBean implements PersonService {public void save(){System.out.println("我是save()");}}
package cn.anycall.service.impl;public class PersonServiceBeanFactory {public static PersonServiceBean createPersonServiceBean(){return new PersonServiceBean();}public PersonServiceBean createPersonServiceBean2(){return new PersonServiceBean();}}
<?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 name="personService" factory-method="createPersonServiceBean"></bean> <!-- 使用实例工厂方法实例化 --> <bean name="personServiceFactory" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"></bean></beans>
package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.anycall.service.PersonService;public class SpringTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void instanceSprint(){ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});PersonService persionService = (PersonService)ctx.getBean("personService");persionService.save(); persionService = (PersonService)ctx.getBean("personService2");persionService.save(); persionService = (PersonService)ctx.getBean("personService3");persionService.save();}}