【问题标题】:How to bind a Data Source?如何绑定数据源?
【发布时间】:2015-05-15 14:30:31
【问题描述】:

我遇到了一个错误 -

org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'netLogsDao': Injection of resource dependencies failed; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'abcDataSource' defined in 
com.kinsale.submclrmgr.config.DataConfig: Bean instantiation via factory 
method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[javax.sql.DataSource]: Factory method 'abcDataSource' threw exception; 
nested exception is 
org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: 
Failed to look up JNDI DataSource with name 'dataSource/abc'; nested 
exception is javax.naming.NameNotFoundException: Name [dataSource/abc] is 
not bound in this Context. Unable to find [dataSource].

我试图弄清楚如何绑定我的数据源,但是我发现的文档没有帮助。有一个 JndiTemplate 可用于绑定数据源,传递字符串名称和对象对象的参数,但我找不到任何关于该对象的信息。

我绑定到数据源名称的对象是什么?它是我正在创建的 Bean 的名称还是 context.xml 中资源的名称?

这是 Bean -

@Bean(name="abcDataSource")
public DataSource abcDataSource(){

JndiDataSourceLookup jndi = new JndiDataSourceLookup();
jndi.setResourceRef(true);

return jndi.getDataSource("dataSource/abc");

这里是bean的使用——

@Resource(name="abcDataSource")
private DataSource ds;

这里是 context.xml 条目(我确实有正确的用户名和密码)-

<Resource 
name="dataSource/abc" 
auth="Container"
type="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
factory="org.apache.naming.factory.BeanFactory"
URL="jdbc:mysql://abc/db_netlogs" />

【问题讨论】:

  • 请提供相关元素和代码以复制您的问题。
  • 你在用弹簧吗?
  • 是的,我正在使用 Spring。

标签: java spring jdbc jndi sqldatasource


【解决方案1】:

为了使它工作,以下依赖项需要在 pom 中。

    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.17</version>
    </dependency>

我添加后,一切正常。

【讨论】:

    猜你喜欢
    • 2013-02-13
    • 2013-07-17
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多