待实例化的Bean
package org.zln.module.test2.service.impl; import org.apache.log4j.Logger; import org.zln.module.test2.service.PersonService; /** * Created by coolkid on 2015/6/6 0006. */ public class PersonServiceBean implements PersonService { private Logger logger = Logger.getLogger(PersonServiceBean.class); @Override public void save(){ logger.debug("我是save()方法"); } /** * 静态工厂方法 * @return 实例 */ public static PersonServiceBean getPersonServiceBean(){ return new PersonServiceBean(); } /** * 静态工厂方法 * @return 实例 */ public PersonServiceBean getPersonServiceFactory(){ 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 id="personService" class="org.zln.module.test2.service.impl.PersonServiceBean"/> <!--静态工厂方法实例化--> <bean id="personService2" class="org.zln.module.test2.service.impl.PersonServiceBean" factory-method="getPersonServiceBean"/> <!--实例工厂方法--> <bean id="personServiceFactory" class="org.zln.module.test2.service.impl.PersonServiceBean"/> <bean id="personService3" factory-bean="personServiceFactory" factory-method="getPersonServiceFactory"/> </beans>