请稍等 ...
×

采纳答案成功!

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

AreaDaoTest运行出现问题

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name ‘dataSource’ defined in class path resource [spring/spring-dao.xml]: Could not resolve placeholder ‘jdbc.driver’ in value "jdbc.driver";nestedexceptionisjava.lang.IllegalArgumentException:Couldnotresolveplaceholder′jdbc.driver′invalue"{jdbc.driver}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driver' in value "jdbc.driver";nestedexceptionisjava.lang.IllegalArgumentException:Couldnotresolveplaceholderjdbc.driverinvalue"{jdbc.driver}"
二月 17, 2020 7:32:40 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@115b42e] to prepare test instance [com.cl.shop.dao.AreaDaoTest@291ae]
java.lang.IllegalStateException: Failed to load ApplicationContext图片描述

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

插入代码

1回答

翔仔 2020-02-18 00:29:53

同学好,你的jdbc.properties是不是没写呀,视频里是写了的,所以没有替换spring-dao.xml里面的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
    <!-- 配置整合mybatis过程 -->
    <!-- 1.配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 2.数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
 
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30" />
        <property name="minPoolSize" value="10" />
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false" />
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000" />
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2" />
    </bean>
 
    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 扫描entity包 使用别名 -->
        <property name="typeAliasesPackage" value="com.imooc.o2o.entity" />
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>
 
    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.imooc.o2o.dao" />
    </bean>
</beans>

jdbc.properties

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=230230


0 回复 有任何疑惑可以回复我~
  • 提问者 C哩C哩1 #1
    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [spring/spring-dao.xml]: Could not resolve placeholder 'jdbc.driver' in value "${jdbc.driver}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driver' in value "${jdbc.driver}"
    
    
    老师,spring-dao.xml和jdbc.properties都替换成了你这个文件,还是报如上的错误   怎么回事啊。。。。。。。。。。。。。
    回复 有任何疑惑可以回复我~ 2020-02-18 10:30:04
  • 翔仔 回复 提问者 C哩C哩1 #2
    同学好,需要提供你的目录结构,感觉你的jdbc.properties放的位置不对,导致spring-dao.xml没读进来。或者如果着急,你可以直接再spring-dao.xml里面把${jdbc.driver}之类的,直接替换成jdbc.properties对应变量的值,比如这里直接写成 <property name="driverClass" value="com.mysql.jdbc.Driver" />
    回复 有任何疑惑可以回复我~ 2020-02-19 02:12:02
  • 提问者 C哩C哩1 回复 翔仔 #3
    我的jdbc.properties是放在src/main/resources目录结构下的 ,也按照上面你说的写成的这种格式<property name="driverClass" value="com.mysql.jdbc.Driver" /> 发现还是报错。
    回复 有任何疑惑可以回复我~ 2020-02-20 09:26:02
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信