Spring配置通过动态工厂方法创建的bean
创建的类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.neuedu.bean; import java.util.HashMap; import java.util.Map; public class InstanceFactory { private Map<String,Book> map=null; { map=new HashMap<String,Book>(); map.put("book1",new Book("asdas", 20.0)); map.put("book2", new Book("cxvxcv", 110.3)); } public Book getBook(String id) { return map.get(id); } }
applicationContext.xml中的配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<bean id="instanceFactory" class="com.neuedu.bean.InstanceFactory"></bean>
<bean id="bookFromInstanceFactory" factory-bean="instanceFactory" factory-method="getBook">
<constructor-arg value="book1"></constructor-arg>
</bean>