请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

test中加载Spring配置文件失败

老师,我按自己的思路重新配置了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

后来网上查了一下,加上红框中的注解就不会报错,运行正常了

https://img1.sycdn.imooc.com//szimg/5a7da6a600016df706710345.jpg

文件的目录是https://img1.sycdn.imooc.com//szimg/5a7da6bb0001365803240578.jpg

老师你是在哪句代码里或哪部操作代替了这个注解呢?麻烦老师解答下。


正在回答 回答被采纳积分+3

3回答

提问者 慕粉3796389 2018-02-11 08:58:38

<?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>


0 回复 有任何疑惑可以回复我~
翔仔 2018-02-10 15:43:57

同学好,同学需要从整合在一块之后遇到的错误里出发,这里的关键语句是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,不过第二种确实逻辑上更容易理解,更加清晰,同学的悟性很高,分析得很不错 :)---


0 回复 有任何疑惑可以回复我~
  • 提问者 慕粉3796389 #1
    老师是因为你分层写的原因吗?后来我发现WebAppConfiguration里面是空值也是可以正常运行的
    回复 有任何疑惑可以回复我~ 2018-02-10 18:49:55
  • 翔仔 回复 提问者 慕粉3796389 #2
    不是呢,是通过web.xml启动的spring容器 同学的这种方式是通过注解方式启动的
    回复 有任何疑惑可以回复我~ 2018-02-10 20:26:33
  • 提问者 慕粉3796389 回复 翔仔 #3
    但是我在web.xml里面是写了
    <servlet>
    		<servlet-name>spring-mvc</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:applicationContext.xml</param-value>
    		</init-param>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>spring-mvc</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    可能这个问题对我来说有点难了~~
    回复 有任何疑惑可以回复我~ 2018-02-10 21:37:13
wexin_Mr_NoBody 2018-02-10 11:45:06

看了下你的问题,应该是jdbc.properties配有妹纸,找不到相关的资源。你看下你的jdbc文件存放的位置在哪,再到spring-dao.xml里找下以下截图中的参数配了没有
https://img1.sycdn.imooc.com//szimg/5a7e6a6b0001ddf106720072.jpg

0 回复 有任何疑惑可以回复我~
  • 提问者 慕粉3796389 #1
    jdbc.properties在main/resources下的,spring-dao里面的配置我也写在applicationcontext.xml里面了的,问题应该是test下面不能读取mai下面的jdbc.properties,但是我不知道老师没有用那个注解是怎么实现的
    回复 有任何疑惑可以回复我~ 2018-02-10 12:01:08
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信