Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'datasource' must be of type [javax.activation.DataSource], but was actually of type [com.mchange.v2.c3p0.ComboPooledDataSource]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:348)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1048)
at study.BeanTest.main(BeanTest.java:12)
spring
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:348)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1048)
at study.BeanTest.main(BeanTest.java:12)
spring
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.activation.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanTest {
public static void main(String[] args)throws Exception {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource ds=ctx.getBean("datasource",DataSource.class);//这两句话有问题
Connection conn=((Statement) ds).getConnection();//
PreparedStatement pstmt=conn.prepareStatement("insert into news_inf values(null,?,?);");
pstmt.setString(1, "weeqweqw ");
pstmt.setString(2, "sdfsdfsdfsdf");
pstmt.executeUpdate();
if(pstmt!=null){
pstmt.close();};
if(pstmt!=null){
conn.close();
};
}}
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/javaee"/>
<property name="user" value="root"/>
<property name="password" value="flight"/>
<property name="minPoolSize" value="1"/>
<property name="maxPoolSize" value="10"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/></bean>
</beans>