spring getBean() 的问题

寒风中游泳 2012-08-20 05:47:40
Spring 掌握的不好,不明白是什么地方错了,希望各位高人解释详细点。到底是找不到什么呢?

代码如下:
public class SpringContextUtil {

private static ApplicationContext context;

public static ApplicationContext getApplicationContext() {
return context;
}

public static Object getBean(String name) {
if (context == null) {
Object bean = DefaultBeanFactory.getBean(name); //一直都是在这里报错
if (bean == null) {
return null;
} else {
return bean;
}
}
return context.getBean(name);
}

public static void setApplicationContext(ApplicationContext acx) {
context = acx;
}
}


报错信息:
org.springframework.beans.FatalBeanException: Unable to find resource for specified definition. Group resource name [classpath*:beanRefFactory.xml], factory key [default]


上面的DefaultBeanFactory引用的是permission.jar com.individual.common.permission.util.DefaultBeanFactory
...全文
224 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xki1314 2012-08-20
  • 打赏
  • 举报
回复
项目启动的时候,先把包含bean的xml文件进行加载
寒风中游泳 2012-08-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
看一下这个:http://longzhun.iteye.com/blog/1024959
[/Quote]

一定要实现ApplicationContextAware这个接口吗 ?
我们项目里侃别人都是这么用的啊 也没实现接口,没继承。
hundsun_2008 2012-08-20
  • 打赏
  • 举报
回复
看一下这个:http://longzhun.iteye.com/blog/1024959
寒风中游泳 2012-08-20
  • 打赏
  • 举报
回复
1楼说的我用过,但这里不用此方法啊。
现在这个是个工具类,项目里可能好几个地方都在用。
  • 打赏
  • 举报
回复
我记得应该这么获取吧
new ClassPathXmlApplicationContext("applicationContext.xml").getBean("beanId");

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧