Spring中使用简单工厂模式去管理bean的分析
public class BeanFactory {
public Object getBean(String name) {
if ("cat".equals(name)) {
return new Cat();
}else if ("dog".equals(name)) {
return new Dog();
}else{
return null
}
}
}
public class BeanFactory {
private Map map;
public void init() {
map.put("cat",new Cat());
map.put("cow",new Cow());
map.put("dog",new Dog());
}
public Object getBean(String name) {
return map.get(name);
}
}
public class BeanFactory {
private Map map;
public void init() {
//读取配置文件
}
public Object getBean(String name) {
return map.get(name);
}
}
<bean id="userService" class="com.hgd.spring.service.UserServiceImpl">
<property name="properties">
<props>
<prop key="h1">23</prop>
<prop key="h2">23</prop>
<prop key="h3">25</prop>
</props>
</property>
</bean>