1. 程式人生 > 實用技巧 >Cannot find class [com.alibaba.druid.pool.DruidDataSuorce]

Cannot find class [com.alibaba.druid.pool.DruidDataSuorce]

八月 06, 2020 3:52:33 下午 org.springframework.context.support.AbstractApplicationContext refresh

警告: Exception encountered during context initialization - cancelling refresh attempt:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookService': Unsatisfied dependency expressed through field 'bookDao';

nested exception is org.springframework.beans.factory.CannotLoadBeanClassException:

Cannot find class [com.alibaba.druid.pool.DruidDataSuorce] for bean with name 'DataSource' defined in class path resource [Beans1.xml];

nested exception is java.lang.ClassNotFoundException: com.alibaba.druid.pool.DruidDataSuorce

(這是我在自學spring整合JdbcTemplate時,運行了一個例子出現的錯誤,花了兩天才解決,僅供參考)

1、出現這種錯誤首先你要去檢查你有沒有匯入druid的jar包

阿里巴巴druid.jar包下載地址:https://mvnrepository.com/artifact/com.alibaba/druid

2、如果jar包匯入沒問題,你先去檢視你的MySQL版本是不是5+

如果版本是5的話你應該匯入和版本一致的mysql-connector的jar包,例如:mysql-connector-java-5.1.39-bin.jar

而如果你的MySQL版本是5+新版本的話那你就要匯入新版本的mysql-connector的jar包,例如:mysql-connector-java-8.0.19.jar

3、如果還是沒用的話,那你就要去檢查你的spring配置了(也就是xml檔案),比如你的配置中有某段程式碼是複製貼上過來的,那你最好自己重新敲一遍,複製貼上有可能會導致空格出錯,不懂的話可以看下我下面的這個小例子我就是因為這個原因,搞了兩天才搞出來 =_=......

mysql8.0新版本如何配置連線池(一個簡單的JdbcTemplate小例子來說明)

(1)引入jar包,這些jar包有一部分要用到(在這個例子)

(2)開啟資料庫(我這裡的資料庫是spring_jdbctemplate_demo1,其中有一個表teacher

(3)建立這些類、xml和外部屬性檔案

建立BookService類

 1 @Service  //使用註解方式建立物件
 2 public class BookService {
 3     
 4     //注入dao
 5     @Autowired  //註解注入屬性
 6     private BookDao bookDao;
 7     
 8     //插入方法
 9     public void addTeacher(Teacher teacher) {
10         bookDao.add(teacher);
11     }
12 }

建立BookDao介面

1 public interface BookDao {
2 
3     //插入方法
4     void add(Teacher teacher);
5 
6 }

建立BookDaoImpl實現類

 1 @Repository  //使用註解方式建立物件
 2 public class BookDaoImpl implements BookDao{
 3     
 4     //注入JdbcTemplate
 5     @Autowired  //註解注入屬性
 6     private JdbcTemplate jdbcTemplate;
 7 
 8     //插入方法
 9     @Override
10     public void add(Teacher teacher) {
11         
12         //1、建立sql語句
13         String sql = "insert into teacher values(?,?,?)";
14         
15         //2、呼叫方法實現
16         Object[] array = {teacher.getTeacherId(), teacher.getTeacherName(), 
17                 teacher.getDepartmentID()};
18         int update = jdbcTemplate.update(sql, array);
19         System.out.println(update);
20     }
21 }

建立表teacher的專門類,類裡面有表teacher中的全部屬性以及屬性對應的get或set方法

 1 public class Teacher {
 2 
 3     private String ID;
 4     private String name;
 5     private String address;
 6     
 7     public String getTeacherId() {
 8         return ID;
 9     }
10     public void setTeacherId(String ID) {
11         this.ID = ID;
12     }
13     public String getTeacherName() {
14         return name;
15     }
16     public void setTeacherName(String name) {
17         this.name = name;
18     }
19     public String getDepartmentID() {
20         return address;
21     }
22     public void setDepartmentID(String address) {
23         this.address = address;
24     }
25 }

建立測試類

 1 public class Test {
 2 
 3     @org.junit.Test
 4     public void test() {
 5         ApplicationContext context = 
 6                          new ClassPathXmlApplicationContext("Beans1.xml");
 7         BookService bookService = context.getBean("bookService", BookService.class);
 8         System.out.println(bookService);
 9         Teacher teacher = new Teacher();
10         teacher.setTeacherId("2022");
11         teacher.setTeacherName("佳奧");
12         teacher.setDepartmentID("004");
13         
14         bookService.addTeacher(teacher);
15     }
16 }

建立xml配置檔案

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                               http://www.springframework.org/schema/beans/spring-beans.xsd
 7                               http://www.springframework.org/schema/context 
 8                               http://www.springframework.org/schema/context/spring-context.xsd"> 
 9     
10     <!-- 元件掃描 -->
11     <context:component-scan base-package="com.spring.jdbctemplate.service, 
12                                           com.spring.jdbctemplate.dao">
13     
14     </context:component-scan>
15     
16     
17     <!-- 引入外部屬性檔案 -->
18     <context:property-placeholder location="classpath:jdbc.properties"/>
19     
20     
21     <!-- 直接配置連線池 -->
22     <bean id="DataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
23         
24         <property name="url" value="${prop.url}" />
25         
26         <property name="username" value="${prop.username}" />
27         
28         <property name="password" value="${prop.password}" />
29         
30         
31         <property name="driverClassName" value="${prop.driverClass}" />
32     </bean>
33     
34 
35     <!-- JdbcTemplate物件 -->
36     <bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
37 
38         <!-- 注入dataSource -->
39         <property name="dataSource" ref="DataSource"></property>
40     </bean>
41     
42     
43 </beans>

建立一個外部屬性檔案,點選src(因為我是用eclipse),右鍵-->NEW-->Flie-->Finish,然後在檔案裡面寫下連線連線資料庫的代

連線資料庫程式碼如下:

1 prop.driverClass = com.mysql.cj.jdbc.Driver  //這個可配置也可不配置
2 prop.url = jdbc:mysql:///spring_jdbcTemplate_demo1?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
3 prop.username = *******
4 prop.password = *******

(OK,你可以去嘗試了,我也是小白,希望能幫到你,加油)