java - NoSuchBeanDefinitionException : No unique bean of type [javax.activation.DataSource] 已定义:预期为单个 bean,但发现 0:

标签 java spring spring-mvc

嘿我无法连接到数据库mysql 我在 Windows 8 64 上使用 spring 工具套件和 phpMyAdmin 以及 xampp

这是我的异常(exception):

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);

在我遇到异常后请帮忙

最佳答案

这样做:

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

您使用了错误的数据源:异常显示javax.activation.DataSource

关于java - NoSuchBeanDefinitionException : No unique bean of type [javax.activation.DataSource] 已定义:预期为单个 bean,但发现 0:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24327608/

相关文章:

mysql - Spring JPA 未连接到多个数据库 : Saying entity managers found 2

java - 从 excel : suggestions for design or approach for better performance 批量加载 Hibernate

java - Spring MVC 中是否可以为请求提供 void 处理程序?

java - 如何使用自定义类加载器反序列化对象?

java - Android 以编程方式插入的联系人未链接到我的应用程序

spring - 在 Spring Boot 中为自定义 Controller 方法启用 HAL 序列化

Spring:/** 和/* 关于路径的区别

java - 将过滤器添加到 web.xml 后,一个或多个过滤器无法启动

发生多个匹配时的java正则表达式首选项

java - 设置为 View.VISIBLE 后,Android Button 需要点击几次才能工作