最近在学SSM在学到spring整合mybatis时遇到了一个错误困扰了一下午
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
这是开始的配置,因为是从mybatis-config.xml中复杂的所以没发现
<property name="driver" value="${jdbc.driver}"/>中要使用
driverClassName导致出现了一系列奇怪的错误
三月 03, 2023 4:19:40 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
三月 03, 2023 4:19:40 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6ba0ac, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1fa3a9d, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@ebc39e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4bee09, org.springframework.test.context.transaction.TransactionalTestExecutionListener@16c48ac, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1deb50e, org.springframework.test.context.event.EventPublishingTestExecutionListener@19963d8]
三月 03, 2023 4:19:41 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'bookMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookMapper' defined in file [F:\idea_yuanma\chapter14\target\classes\com\sjb\dao\BookMapper.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver': no matching editors or conversion strategy found
三月 03, 2023 4:19:41 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@ebc39e] to prepare test instance [com.sjb.service.BookServiceTest@1bbc4e]
java.lang.IllegalStateException: Failed to load ApplicationContext
当把dataSource改成下面这样时就OK了
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>