浏览 6928 次
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2005-09-19
static { String[] paths = {"classpath*:WEB-INF/applicationContext-hibernate.xml"}; ctx = new ClassPathXmlApplicationContext(paths); if(ctx == null){ System.out.println("ctx is null"); } else{ System.out.println("ctx is not null"); } System.out.print(ctx.getBean("agreementDAO").toString()); } applicationContext-hibernate.xml文件片段 <!-- ***** AGREEMENT SERVICE *****--> <bean id="agreementService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref local="myTransactionManager"/></property> <property name="target"><ref local="agreementTarget"/></property> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly,-AgreementException</prop> <prop key="save*">PROPAGATION_REQUIRED,-AgreementException</prop> <prop key="update*">PROPAGATION_REQUIRED,-AgreementException</prop> <prop key="delete*">PROPAGATION_REQUIRED,-AgreementException</prop> </props> </property> </bean> <!-- AgreementTarget primary business object implementation --> <bean id="agreementTarget" class="com.pms.service.spring.AgreementServiceSpringImpl"> <property name="agreementDAO"><ref local="agreementDAO"/></property> </bean> <!-- DAO object: Hibernate implementation --> <bean id="agreementDAO" class="com.pms.service.dao.hibernate.AgreementHibernateDAO"> <property name="sessionFactory"><ref local="mySessionFactory"/></property> </bean> 运行时的错误信息如下: Exception in thread "main" java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at junit.framework.TestSuite.createTest(TestSuite.java:131) at junit.framework.TestSuite.addTestMethod(TestSuite.java:114) ctx is not null at junit.framework.TestSuite.<init>(TestSuite.java:75) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTest(RemoteTestRunner.java:360) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:398) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'agreementDAO' is defined: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans []; root of BeanFactory hierarchy at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:332) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:672) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:159) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:496) at com.pms.service.dao.BaseDAOTestCase.<clinit>(BaseDAOTestCase.java:53) ... 11 more 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2005-09-20
你这个代码有什么用?ctx是new出来的,可能为null么?
ctx = new ClassPathXmlApplicationContext(paths); if(ctx == null){ System.out.println("ctx is null"); } else{ System.out.println("ctx is not null"); } |
|
返回顶楼 | |
发表时间:2005-09-20
可为什么找不到bean呢?
|
|
返回顶楼 | |
发表时间:2005-09-20
既然你检查ctx建立是否正确的代码是错的,
那么就有理由怀疑ctx不正确 你打开spring的log看看 |
|
返回顶楼 | |
发表时间:2005-09-21
楼上说的spring的log在哪里?
我改了代码: BaseDAOTestCase .java public abstract class BaseDAOTestCase extends TestCase { protected final Log log = LogFactory.getLog(getClass()); protected final static ApplicationContext ctx; protected ResourceBundle rb; static { String[] paths = {"classpath*:WEB-INF/applicationContext-hibernate.xml"}; ctx = new ClassPathXmlApplicationContext(paths); } AgreementDAOTest.java public class AgreementDAOTest extends BaseDAOTestCase{ private IAgreementDAO dao = null; private List agreementList = null; private TagentProductPara agentProductPara = null; protected void setUp() throws Exception { super.setUp(); dao = (IAgreementDAO) ctx.getBean("agreementDAO"); } applicationContext-hibernate.xml <!-- DAO object: Hibernate implementation --> <bean id="agreementDAO" class="com.pms.service.dao.hibernate.AgreementHibernateDAO"> <property name="sessionFactory"><ref local="mySessionFactory"/></property> </bean> 运行junit测试 AgreementDAOTest.java 进入了junit的届面,错误提示如下: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'agreementDAO' is defined: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans []; root of BeanFactory hierarchy at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:332) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:672) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:159) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:496) at com.pms.service.dao.AgreementDAOTest.setUp(AgreementDAOTest.java:39) at junit.framework.TestCase.runBare(TestCase.java:125) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186) |
|
返回顶楼 | |
发表时间:2005-09-21
配置log4j
|
|
返回顶楼 | |
发表时间:2007-07-05
meetyou930 写道 可为什么找不到bean呢?
配置文件放在 WEB-INF目录下,测试时要写绝对路径,不然找不到的,呵呵! |
|
返回顶楼 | |