Out容器JNDI数据源 [英] Out of container JNDI data source

查看:138
本文介绍了Out容器JNDI数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java SE应用程序中使用JNDI配置DataSource。最好的方法是什么?

I would like to configure a DataSource using JNDI in a Java SE app. What is the best way to do this?

到目前为止,我遇到了两个项目:

So far, I've come across 2 projects:


  1. Apache命名。项目页面有一个配置数据源的具体示例,但看起来该项目已经超级旧,不再有效。

  2. JBossNS 。看起来很容易使用 LocalOnlyContextFactory 配置本地JNDI,但我没有找到任何关于如何实际配置数据源的文档。

  1. Apache Naming. The project page has a specific example for configuring a data source, but it looks like the project is super old and no longer active.
  2. JBossNS. It looks like it's easy to configure a local-only JNDI using LocalOnlyContextFactory, but I haven't found any docs on how to actually configure a data source.

如果可能的话,我还想用JTA事务管理器配置数据源(使用JOTM?)。

If possible, I would like to also configure the data source with a JTA transaction manager (using JOTM?).

推荐答案

为什么要使用JNDI呢?如果你有一个提供者并且有依赖注入等替代方案(IoC:通过 Spring 或 Guice )。

Why are you using JNDI for this? It's not that it's a bad solution if you have a provider but there are alternatives such as dependency injection (IoC: via Spring or Guice).

此处介绍了Spring JDBC数据访问。最棒的是,您可以使用Spring在代码中注入 DataSource

The Spring JDBC data access is described here. The great thing is that you can use Spring to inject a DataSource into your code:

<bean class="com.my.Persister">
    <property name="dataSource" ref="dataSource" />
</bean>

可以使用JNDI查找定义数据源:

The data source can be defined using a JNDI-lookup:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource" /> 

在测试环境中,您可以直接注入数据源:

In a test environment, you could inject the data source directly:

<bean id="dataSource" class="apache.db.PoolingDataSource">
    <!-- config goes here -->
</bean>

这篇关于Out容器JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆