For SSM integration, a Cannot resolve reference to bean 'com.mysql.cj.jdbc.Driver' or No bean named 'database driven full class name' solution appears

After building a complete SSM framework, I test the addition, deletion, modification and query of the database in the test class. The following is the test code:

    @Test
    public void insert() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        EmployeeMapper employeeMapper = applicationContext.getBean(EmployeeMapper.class);
        System.out.println(employeeMapper);
    }

Then, the following error messages appear when running

November 01, 2021 2:44:07 PM org.springframework.context.support.AbstractApplicationContext refresh
Warning: exception enumerated during context initialization - canceling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'pooledDataSource' defined in class path resource [applicationContext.xml]: Cannot resolve reference to
bean 'com.mysql.cj.jdbc.Driver' while setting bean property 'driverClassName'; nested exception is
\org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
'com.mysql.cj.jdbc.Driver' available

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'pooledDataSource' defined in class path resource [applicationContext.xml]: Cannot resolve reference to
bean 'com.mysql.cj.jdbc.Driver' while setting bean property 'driverClassName'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
'com.mysql.cj.jdbc.Driver' available

  • However, according to its error prompt, I find the data source configuration in application.xml (i.e. spring configuration file) (at this time, I use c3p0 data source)
  • At first, I thought there was a problem with the configuration of the externally referenced database connection information, that is, the following paragraph:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=1234
jdbc.url=jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&characterEncoding=utf8
  • However, I still have this problem after modification. Later, I thought there was a problem with the data source (I changed the Druid data source) and modified the configuration information of the data source. The following paragraph:
<bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource" >
  <property name="url" value="${jdbc.url}"/>
  <property name="driverClassName" ref="${jdbc.driver}"/>
  <property name="username" ref="${jdbc.username}"/>
  <property name="password" ref="${jdbc.password}"/>
</bean>

But there's still a problem

Question:

Then I open my learning materials and find the spring configuration file matched by the teacher. I copy it and paste it on my configuration file. The artifact is that it runs successfully

Then I copied the data source configured by the teacher to my own configuration file, commented out my own data source configuration, and ran successfully. The following is my configured data source

    <context:property-placeholder location="classpath:jdbc.properties" />
    <context:component-scan base-package="com.atguigu" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!-- I wrote this myself, test: report errors -->
<!--        <bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource" >-->
<!--            <property name="url" value="${jdbc.url}"/>-->
<!--            <property name="driverClassName" ref="${jdbc.driver}"/>-->
<!--            <property name="username" ref="${jdbc.username}"/>-->
<!--            <property name="password" ref="${jdbc.password}"/>-->
<!--        </bean>-->
    
    <!-- This is copied from the teacher's code,test: no problem -->
        <bean id="pooledDataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="url" value="${jdbc.url}"/>
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>

Then what do I want to explain? Because something magical happened, I ran the teacher's data source configuration, and there was no problem

Solution to error reporting:

Then run the data source configured by myself, and then the error described in my title appears. My tm Baidu search is almost rotten, and I can't find the error related to it

I searched < < no bean named 'full class name of database driver' > > other people's errors are either SpringBoot errors or other errors

Then I compared my own data source configuration with the teacher's data source configuration. There is no difference at all, so is this Metaphysics? Ask the God who knows what caused it

The following is the configuration comparison

Well, if you have some questions similar to mine... I hope you can find this article
Then you can be as crazy as me, hahaha

Keywords: Java Maven Mybatis Spring Spring MVC

Added by pngtest on Mon, 01 Nov 2021 12:50:31 +0200