Spring中说了,一个bean实现BeanFactoryAware,就可以取得这个bean所在的上下文的BeanFactory:
<?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.0.xsd">
<bean name="codehelpcommand" class="com.zyp.finance.server.CodeHelpCommand"/>
<bean name="baseServiceImpl" class="com.zyp.finance.server.BaseServiceImpl"/>
</beans>
上面的baseServiceImpl实现了BeanFactoryAware接口,按理应该可以获取到codehelpcommand这个bean的,结果下面的小测试出了问题:
package com.zyp.finance.server;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.zyp.finance.client.BaseService;
import com.zyp.finance.client.RequestData;
/**
* The server side implementation of the RPC service.
*/
public class BaseServiceImpl extends RemoteServiceServlet implements
BaseService, BeanFactoryAware{
private BeanFactory beanFactory;
public RequestData baseServer(RequestData data) {
String action = data.getActionName();
ICommand command = fetchCommand(action);
RequestData response = command.execute(data);
return response;
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("arg0="+arg0);//1:打印出了2个bean
beanFactory = arg0;
}
//通过spring获取对应的bean处理
public ICommand fetchCommand(String action){
System.out.println("action="+action);
if(this.beanFactory==null){
System.out.println("this.beanFactory is null");//2:打印出了这句,beanFactory为空。
}
Object bean = this.beanFactory.getBean(action);
if(bean==null){
System.out.println("error,no such bean:"+action);
return null;
}
else return (ICommand) bean;
}
}
启动服务器的时候,打印出了1处的context中的其他bean:
arg0=org.springframework.beans.factory.support.DefaultListableBeanFactory@a9255c: defining beans [codehelpcommand,baseServiceImpl]; root of factory hierarchy
调用2处的方法的时候,报错,beanFactory为空。
当初整合GWT和Spring的时,想起同事写了个单例来存放beanFactory,按照那个思路改了下代码,就好了:
package com.zyp.finance.server;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class ContextUtil implements BeanFactoryAware{
private static BeanFactory beanFactory;
private static ContextUtil instance = new ContextUtil();
public BeanFactory getBeanFactory() {
return beanFactory;
}
private ContextUtil(){
}
public static ContextUtil getInstance(){
return instance;
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
beanFactory = arg0;
}
}
将这个ContextUtil放在xml中去加载:
<?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.0.xsd">
<bean name="codehelpcommand" class="com.zyp.finance.server.CodeHelpCommand"/>
<bean name="baseServiceImpl" class="com.zyp.finance.server.BaseServiceImpl"/>
<bean name="contextUtil" class="com.zyp.finance.server.ContextUtil"/>
</beans>
BaseServiceImpl获取,并且这个类自己不用再实现BeanFactoryAware了。
package com.zyp.finance.server;
import org.springframework.beans.factory.BeanFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.zyp.finance.client.BaseService;
import com.zyp.finance.client.RequestData;
public class BaseServiceImpl extends RemoteServiceServlet implements
BaseService{
private BeanFactory beanFactory = ContextUtil.getInstance().getBeanFactory();
//通过spring获取对应的bean处理
public ICommand fetchCommand(String action){
Object bean = this.beanFactory.getBean(action);
//成功获取到了,其他代码省略。
}
}
至于为什么会这样。。不清楚了。。明明bean都是默认单例的。。小纠结~挖个坑,二天来埋
分享到:
相关推荐
Spring中ApplicationContext和beanfactory区别.rar
Spring的BeanFactory的接口的中文解释
NULL 博文链接:https://bjameng.iteye.com/blog/1468057
NULL 博文链接:https://ylxy3058.iteye.com/blog/2223489
主要介绍了简单了解Spring中BeanFactory与FactoryBean的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://lgd-java2eye.iteye.com/blog/756599
spring之beanfactory 的一些基本知识与其关系使用
今天小编就为大家分享一篇关于spring中的BeanFactory与FactoryBean的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
对BeanFactory的相关所有接口的功能的关键点进行了总结描述,中文版本
主要给大家介绍了关于Spring中BeanFactory与FactoryBean接口的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
本篇文章主要介绍了Spring中BeanFactory解析bean详解 ,详细的介绍了使用BeanFactory对bean进行解析的实例,有兴趣的可以了解一下。
spring3中实例化BeanFactory的3中方法
NULL 博文链接:https://364232252.iteye.com/blog/2369489
Spring注解驱动开发第41讲——Spring IOC容器创建源码解析(一)之BeanFactory的创建以及预准备工作(合起来整个过程)
Spring的IoC容器之BeanFactory[定义].pdf
一共近百道题与完整答案Spring框架的优点都有什么?...spring 中的BeanFactory与ApplicationContext的作用和区别?Spring如何实现资源管理?如何在web应用里面配置spring?。。。。。。 适合面试、学习、总结与复习.
BeanFactory是用于访问Spring Bean容器的根接口,是一个单纯的Bean工厂,也就是常说的IOC容器的顶层定义,各种IOC容器是在其基础上为了满
Spring Framework 解压,并将其中的 spring.jar、commons-logging.jar 和 log4j-1.2.15.jar 复制到“D:\java\Spring\lib”目录中,然后在 Eclipse 中建立一个“Springlib”库,将三个文件添加进“Springlib”库中。...
迷你书是《Spring揭秘》的精选版,节选了原书中介绍Spring IoC容器的6个章节。《Spring揭秘》以幽默生动的语言、辅以有趣的故事和典故,循循善诱地阐述了Spring框架的方方面面。针对Spring框架的主要功能以及开发者...
Spring教程 1 Spring框架概述 3 Spring是什么? 3 Spring的历史 4 Spring的使命(Mission Statement) 4 Spring受到的批判 4 Spring包含的模块 5 总结 6 Spring的IoC容器 6 用户注册的例子 7 面向接口编程 8 (用户...