test中加载Spring配置文件失败
来源:1-2 开发准备
慕粉3796389
2018-02-09
老师,我按自己的思路重新配置了application.xml,也就是把你的三个Spring合在一起。发现单元测试的时候失败了。
报错为:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [resources/jdbc.properties]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@71329995]]] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@153f5a29: startup date [Fri Feb 09 21:42:51 CST 2018]; root of context hierarchy
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 more
Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [resources/jdbc.properties]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@71329995]]] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@153f5a29: startup date [Fri Feb 09 21:42:51 CST 2018]; root of context hierarchy
at org.springframework.web.context.support.WebApplicationObjectSupport.getWebApplicationContext(WebApplicationObjectSupport.java:112)
at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:128)
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.initContentNegotiationStrategy(ResourceHttpRequestHandler.java:306)
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.afterPropertiesSet(ResourceHttpRequestHandler.java:268)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 40 more
后来网上查了一下,加上红框中的注解就不会报错,运行正常了
文件的目录是
老师你是在哪句代码里或哪部操作代替了这个注解呢?麻烦老师解答下。
3回答
-
慕粉3796389
提问者
2018-02-11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.cjlu.web"></context:component-scan>
<context:component-scan base-package="com.cjlu.entity"></context:component-scan>
<context:component-scan base-package="com.cjlu.service"></context:component-scan>
<!-- 本地jdbc资源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 开启SpringMVC注解模式 -->
<mvc:annotation-driven />
<bean id="viewResolver" name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".html"></property>
</bean>
<!-- 配置数据库源 -->
<bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 配置初始化大小、最小、最大 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="1000"/>
<!-- 获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- mybatis文件配置,扫描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类并注入到Spring的容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.cjlu.dao"/>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 直接访问静态资源 -->
<mvc:resources mapping="/resources/**" location="/resources/jdbc.properties" />
<mvc:default-servlet-handler/>
</beans>
00 -
翔仔
2018-02-10
同学好,同学需要从整合在一块之后遇到的错误里出发,这里的关键语句是Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [resources/jdbc.properties]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@71329995]]] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@153f5a29: startup date [Fri Feb 09 21:42:51 CST 2018];
即加载进来的相关配置并不是跑在咱们常规的WebApplicationContext而是跑在GenericApplicationContext下面。而同学用那个注释就是告诉说这个resources资源文件目录需要加载在WebApplicationContext故而解决了错误。
咱们的工程在web.xml里面将spring的这些xml配置文件整合在一块,而同学是通过另外一种也是很主流的方式来加载spring容器相关配置。两种方式都是没问题的,具体可以参照一个同学发的问题,相信会对同学有收获
https://coding.imooc.com/learn/questiondetail/33020.html
刚刚才找到这个帖子,之前一直没找到,后来翔仔在评论中是这样回这位同学的
---两种实现的本质目的是一致的,都是为了加载WebApplicationContext,不过第二种确实逻辑上更容易理解,更加清晰,同学的悟性很高,分析得很不错 :)---
082018-02-11 -
wexin_Mr_NoBody
2018-02-10
看了下你的问题,应该是jdbc.properties配有妹纸,找不到相关的资源。你看下你的jdbc文件存放的位置在哪,再到spring-dao.xml里找下以下截图中的参数配了没有
012018-02-10
相似问题