springtest用class加载时怎么加载db.spring引入propertiess

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&> spring mvc junit加载属性配置文件解决方法
spring mvc junit加载属性配置文件解决方法
pingangu & &
发布时间: & &
浏览:5 & &
回复:0 & &
悬赏:0.0希赛币
spring mvc junit加载属性配置文件在运行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.springframewornfig.PropertyPlaceholderConfigurer"&
&property&name="locations"&
&value&WEB-INF/config/jdbc.properties&/value&
&/property&
项目可以直接在tomcat中跑起来。但运行junit时报错:
org.springframework.beans.factory.BeanInitializationException:&Could&not&load&&nested&exception&is&java.io.FileNotFoundException:&Could&not&open&ServletContext&resource&[/WEB-INF/config/jdbc.properties]
at&org.springframewornfig.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at&orntext.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
at&orntext.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
at&orntext.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.springframeworntext.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
at&orre.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
at&orre.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at&org.springframewornfig.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
...&19&more
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&spring引用properties文件内容
spring引用properties文件内容
发布时间: 16:52:00
编辑:www.fx114.net
本篇文章主要介绍了"spring引用properties文件内容",主要涉及到spring引用properties文件内容方面的内容,对于spring引用properties文件内容感兴趣的同学可以参考一下。
1.PropertyPlaceholderConfigurer类
它是把属性中的定义的变量(var)替代,spring的配置文件中使用${var}的占位符
&bean&id=&configBean&&class=&org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&&
&&&&&&&&&&&&property&name=&location&&&value&db.properties&/value&&/property&
&bean&id=&dataSource&&class=&mons.dbcp.BasicDataSource&&destroy-method=&close&&
&&&&&property&name=&driverClassName&&&value&${jdbc.driverClassName}&/value&&/property&
&&&&&property&name=&url&&&value&${jdbc.url}&/value&&/property&
&&&&&property&name=&username&&&value&${jdbc.username}&/value&&/property&
&&&&&property&name=&password&&&value&${jdbc.password}&/value&&/property&
db.properties文件
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
2.PropertyOverrideConfigurer类
跟PropertyPlaceholderConfigurer功能一样,不过用法不一样.不用占位符,在属性文件中
直接定义属性的&#20540;,这样就允许有默认&#20540;
&bean&id=&configBean&&class=&org.springframework.beans.factory.config.PropertyOverrideConfigurer&&
&&&&&&&&&&&&property&name=&location&&&value&db.properties&/value&&/property&
&bean&id=&dataSource&&class=&mons.dbcp.BasicDataSource&&destroy-method=&close&&
&&&&&property&name=&driverClassName&&&value&org.hsqldb.jdbcDriver&/value&&/property&
&&&&&property&name=&url&&&value&jdbc:hsqldb:hsql://production:9002&/value&&/property&
&&&&&property&name=&username&&&value&test&/value&&/property&
&&&&&property&name=&password&&&value&123456&/value&&/property&
db.properties文件
在bean实例时,admin,9527将替代test,123456
1)如果需要引用多个属性,将configBean属性改为
2)在ApplactionContext中是自动调用BeanFactoryPostProcessor接口的,如果要在BeanFactory中使用,必须手动添加:
dataSource.username=admin
dataSource.password=9527
&property&name=&locations&&
&value&db.properties&/value&
&value&db1.properties&/value&
&/property&
XmlBeanFactory factory =&new&XmlBeanFactory(new&FileSystemResource(&beans.xml&));
PropertyPlaceholderConfigurer cfg =&new&PropertyPlaceholderConfigurer();
cfg.setLocation(new&FileSystemResource(&jdbc.properties&));
cfg.postProcessBeanFactory(factory);
本文标题:
本页链接:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 spring注入properties 的文章

 

随机推荐