如何在没有app服务器的情况下欺骗数据源的jndi查找 [英] How can I spoof a jndi lookup for a datasource without a app server

查看:107
本文介绍了如何在没有app服务器的情况下欺骗数据源的jndi查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一些内部网络应用程序的新功能。这个新代码使用通常由应用服务器(tomcat)提供的数据库连接。

I want to test some new functionality which is part of an internal web app. This new code uses a database connection normally provided by an app server (tomcat).

我不想在我的本地机器上重新创建整个Web应用程序来测试新的代码,因为我只需要运行一个函数。

I do not want to recreate the entire web app on my local machine to test the new code, since I only need to run one function.

有没有人知道如何欺骗上下文或数据源来检索数据库配置,而不实际创建服务器上的Web应用程序实例?

Does anyone know how I can 'spoof' a Context, or Datasource, to retrieve the database config, without actually creating a web app instance on a server?

推荐答案

在Spring的帮助下 SimpleNamingContextBuilder 和Apache BasicDataSource ,你可以这样做(我通常在测试中的静态块中有这个)需要JNDI的类):

With the help of Spring SimpleNamingContextBuilder and Apache BasicDataSource, you can do something like this (I usually have this in a static block in test classes that need JNDI):

BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(db_driver_name);
dataSource.setUrl(db_connection_url);
dataSource.setUsername(db_username);
dataSource.setPassword(db_password);
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind(jndi_name, dataSource);
builder.activate();

jndi_name 的值可能如下所示: java:comp / env / jdbc / my-db

The value of jndi_name might look like this: java:comp/env/jdbc/my-db

设置完成后,代码通常看起来通过JNDI建立数据库连接应该可行。上面的代码例如可以使用这个Spring配置:

Once this is set up, code that normally looks up the database connection via JNDI should work. The code above would for example work with this Spring config:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/my-db"/>
</bean>

这篇关于如何在没有app服务器的情况下欺骗数据源的jndi查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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