NoSuchBeanDefinitionException:没有定义[javax.activation.DataSource]类型的唯一bean:期望的单个bean但找到0:

时间:2014-06-20 12:45:05

标签: java spring spring-mvc

Hay不能连接数据库mysql 我在Windows 8 64上使用弹簧工具套件和phpMyAdmin与xampp

这是我的例外:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.activation.DataSource] is defined: expected single bean but found 0: 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
at pl.bnsmedia.tasks.Runner.main(Runner.java:32)

我在app-context.xml的配置

<?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"
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
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<description>Example configuration to get you started.</description>

<context:component-scan base-package="pl.bnsmedia.tasks" />
<context:property-placeholder location="classpath:META-INF/spring/service-config.properties"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:url="jdbc:mysql://localhost:3306/taskManager"
p:driverClassName="com.mysql.jdbc.Driver"
p:username="root"
p:password="sokol"

/>

我有mysql-connecor和spring-jdbc

我正在尝试做一个简单的代码,如:

 ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/app-context.xml");
     DataSource dataSource = context.getBean(DataSource.class);
     System.out.println(dataSource);

我有异常请帮助

1 个答案:

答案 0 :(得分:4)

这样做:

javax.sql.DataSource dataSource = context.getBean(javax.sql.DataSource.class);

您使用的是错误的数据源:例外javax.activation.DataSource