嘿我无法连接到数据库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/