【问题标题】:Cannot find dataSource, even though the dataSource is defined in context.xml即使在 context.xml 中定义了数据源,也找不到数据源
【发布时间】:2015-07-25 05:16:16
【问题描述】:

我为一个特定的 dao 方法声明了以下 bean,以使用与项目其余部分不同的数据源。

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

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

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

这是那个 bean 的用途。

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

这里来自 context.xml(省略了用户和密码,它们已被验证为正确的,如果它们错了就会抛出不同的错误。)我也尝试将它放在上下文中。服务器的xml,放在一个地方而不是另一个地方,并且在两个地方都有。

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

错误是名称未找到异常 -

Caused by: javax.naming.NameNotFoundException: Name [dataSource/kings] is 
not bound in this Context. Unable to find [dataSource].

那么,我的问题是 - 我将它绑定到什么以及如何绑定?

【问题讨论】:

  • 你在web.xml中有对应的resource-ref声明吗?
  • 我加了,没解决问题。

标签: java mysql spring datasource


【解决方案1】:

尝试将resource-ref 添加到您的 web.xml 中

<resource-ref>
    <res-ref-name>dataSource/kings</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

【讨论】:

  • 我忘了说我也这样做了。好吧,我有 resource-env-ref,我在 SO 的其他地方找到了它。我会试试你的代码。
  • 其他一切都保持不变 - 这并没有解决问题。同样的错误。
猜你喜欢
  • 2022-08-04
  • 1970-01-01
  • 2011-04-14
  • 2019-10-11
  • 2011-08-21
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多