日期:2014-05-20  浏览次数:20834 次

spring中bean的初始化问题,路过的大侠们请求你们看一下
测试类:
package com.njty.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DependsHello {
public void display() {
System.out.println("this is class DependsHello depends class Hello!!");
}

public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext();
DependsHello dh = (DependsHello)ac.getBean("dh");
dh.display();
}
}



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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="dh" class="com.njty.aop.DependsHello"></bean>

</beans>

出错信息:
Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:153)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:881)
at com.njty.aop.DependsHello.main(DependsHello.java:13)


搞不明白了,就这么个简单的小例子都错误,还找不到解决办法,很是郁闷。
哪位大侠帮助看一下,在线等,解决就给分,谢谢!

------解决方案--------------------
cglib.jar 加到classpath下了吗
------解决方案--------------------
探讨
引用:
cglib.jar 加到classpath下了吗

cglib.jar是什么东东,其它的都能运行,就这一个不能运行。

------解决方案--------------------
探讨
肯定加入了,因为其它的测试类都能运行呀。

------解决方案--------------------
1.不知道是楼主的Spring版本太低还是,我的版本高了点,ClassPathXmlApplicationContext有下面构造函数么?
探讨
ApplicationContext ac = new ClassPathXmlApplicationContext();

------解决方案--------------------
不知道,看文档。
------解决方案--------------------
看不出来,没理由。
------解决方案--------------------
spring2.5? 但是你的这些配置好像是2.0的
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
------解决方案--------------------
Java code

Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

------解决方案--------------------
可能是Spring的配置文件没有读入,cglib.jar的用处是面向切面,这里不加都可以。。。