报错:Bean 必须为 ‘javax. sql. DataSource‘ 类型

发布于:2025-04-16 ⋅ 阅读:(25) ⋅ 点赞:(0)
<?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">
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>
上述代码出现报错:其中<property name="dataSource" ref="dataSource"></property>Bean ref引用报错为:必须为 'javax. sql. DataSource' 类型

降数据源替换为:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>

仍然出现报错,我的pom.xml文件从网上下载下来的,应该不是包冲突。

后来想想用的jdk版本为17,可能有些高了,降jdk版本为11解决冲突,这个问题如果在设置源的时候有提示,那么应该不是源的问题,可以尝试降低jdk版本