spring文件配置一共有4个applicationContext-resource.xml,applicationContext-dao.xml,applicationContext-service.xml都配在了/web-inf/config/下,还有一个占位符引用的配置文件jdbc.properties,这些资源我想在junit
中通过mock包来测试,总是报错,下面贴出核心代码,向大家求助,尽量不通过更改配置文件的位置来解决.
<beans> 
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/config/jdbc.properties</value>
</property>
</bean><bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="driverType">
<value>${jdbc.driverClassName}</value>
</property>
<property name="URL">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/sinosoft/frameDemo/entity/Employee.hbm.xml</value>
<value>com/sinosoft/frameDemo/entity/Department.hbm.xml</value>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.hbm2ddl.auto">
${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">
${hibernate.cglib.use_reflection_optimizer}
</prop>
<prop key="jdbc.fetch_size">${jdbc.fetch_size}</prop>
<prop key="jdbc.batch_size">${jdbc.batch_size}</prop>
</props>
</property> 
</bean>
</beans>测试代码:public class TestDemo extends AbstractTransactionalDataSourceSpringContextTests {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}@AfterClass
public static void tearDownAfterClass() throws Exception {
}@Override
protected String[] getConfigLocations() {
String[] config = new String[] { "file:web/WEB-INF/config/applicationContext-dao.xml",
"file:web/WEB-INF/config/applicationContext-resource.xml",
"file:web/WEB-INF/config/applicationContext-service.xml" };
return config;
}public void testDao() throws Exception {
applicationContext.getBean("puInfoCacheManager");}
}报错:org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/config/jdbc.properties] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/config/jdbc.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:135)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:179)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:158)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:68)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:472)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:334)
at org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext(AbstractSingleSpringContextTests.java:199)
at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:179)
at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:158)
at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:105)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:87)
at junit.framework.TestCase.runBare(TestCase.java:132)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
附:jdbc.properties 文件怎么配,spring容器都会报错,要么是不能解析,要么是不能加载,要么是找不到....现在我把jdbc.properties放到src下,配置成<value>classpath:jdbc.properties</value>,junit就正常了,为什么放到web-inf下,spring就找不到了,如何让它在web-inf下也能顺利加载呢?

解决方案 »

  1.   

    要保证jdbc.properties在web-inf的classes文件下
      

  2.   

    既然你诚心诚意的问了,那我就大发慈悲的告诉你:
    第一步:在applicationContext-resource.xml中配置导入,连成一条线,好处后面你就知道了
             <import resource="applicationContext-dao.xml"/>
    <import resource="applicationContext-service.xml"/>
    第二步:其他配置保持你的原貌不变,junit测试代码如下:
    private static SessionFactory sessionFactory;
    @BeforeClass
     public static void setUpBeforeClass() throws Exception {
    XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("web/WEB-INF/config/applicationContext-resource.xml"));
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    cfg.setLocation(new FileSystemResource("web/WEB-INF/config/jdbc.properties"));
    cfg.postProcessBeanFactory(factory);
    sessionFactory=(SessionFactory)factory.getBean("sessionFactory");
     }二招搞不定的问题,一般不会出手的,一出手就必定2招内搞定,拿分走人!!!
      

  3.   

    另外敲打下1楼,不要误导别人,真正规范的项目配置文件都是在web-inf下的,好处自己去查,放类路径的做法都是教材书本培训机构写出来骗人的,开源架构不是死的非要写类路径下,就算你丢在茅厕里也能加载到