spring mvc junit加载属性配置文件

kl172 2013-04-17 03:03:56
在运行junit 加载配置文件时,属性配置文件 jdbc.properties一直加载不到。。求帮助!!!

String[] configs = {"file:WebRoot/WEB-INF/config/application-context.xml"}
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc);
context.refresh();

在application-context.xml中包含
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/config/jdbc.properties</value>
</list>
</property>
</bean>


项目可以直接在tomcat中跑起来。但运行junit时报错:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/config/jdbc.properties]
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at com.hisense.hitv.utils.JUnitActionBase.setUp(JUnitActionBase.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/config/jdbc.properties]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
... 19 more

...全文
544 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
billy201 2013-04-25
  • 打赏
  • 举报
回复
怎么解决的,分享一下啊。
kl172 2013-04-22
  • 打赏
  • 举报
回复
问题已解决。
kl172 2013-04-18
  • 打赏
  • 举报
回复
新问题
/** 
	     * 读取spring3 MVC配置文件 
	     */  
	    @BeforeClass  
	 public static void setUp() {  
	        if (handlerMapping == null) {  
	        	String[] configs = {"file:WebRoot/WEB-INF/config/application-context.xml"};  
	            XmlWebApplicationContext context = new XmlWebApplicationContext();  
	            context.setConfigLocations(configs);  
	            MockServletContext msc = new MockServletContext();  
	            context.setServletContext(msc);         
	            context.refresh();  
	            msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);  
	            handlerMapping = (HandlerMapping) context  
	                    .getBean(DefaultAnnotationHandlerMapping.class);  
	            handlerAdapter = (HandlerAdapter) context.getBean(
	            		context.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);     
	        }  
	    }
当运行到 handlerMapping = ...这行时报错。 org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] is defined: expected single bean but found 2: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#1 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:269) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) at com.hisense.hitv.utils.JUnitActionBase.setUp(JUnitActionBase.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 求大神帮忙解决!
lvzg_005 2013-04-18
  • 打赏
  • 举报
回复
kl172 2013-04-18
  • 打赏
  • 举报
回复
问题解决了。是路径问题,我把配置文件里改成./jdbc.properties 。把jdbc.properties文件改放到src下了
lvzg_005 2013-04-18
  • 打赏
  • 举报
回复
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] is defined: expected single bean but found 2: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#1 spring发现了两个同类型的bean->DefaultAnnotationHandlerMapping 通过name获取试试 application的配置文件看看

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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