Java Tomcat 与 Weblogic JNDI 查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:37:22  来源:igfitidea点击:

Tomcat vs Weblogic JNDI Lookup

javatomcatjakarta-eeweblogicjndi

提问by Luther Baker

The Weblogic servers we are using have been configured to allow JNDI datasource names like "appds".

我们使用的 Weblogic 服务器已配置为允许 JNDI 数据源名称,如“appds”。

For development (localhost), we might be running Tomcat and when declared in the <context> section of server.xml, Tomcat will hang JNDI datasources on "java:comp/env/jdbc/*" in the JNDI tree.

对于开发(本地主机),我们可能正在运行 Tomcat,当在 server.xml 的 <context> 部分声明时,Tomcat 会将 JNDI 数据源挂在 JNDI 树中的“java:comp/env/jdbc/*”上。

Problem:in Weblogic, the JNDI lookup is "appds" whilst in Tomcat, it seems that that I must provide the formal "java:comp/env/jdbc/appds". I'm afraid the Tomcat version is an implicit standard but unfortunately, I can't change Weblogic's config ... so that means we end up with two different spring config files (we're using spring 2.5) to facilitate the different environments.

问题:在 Weblogic 中,JNDI 查找是“appds”,而在 Tomcat 中,似乎我必须提供正式的“java:comp/env/jdbc/appds”。恐怕 Tomcat 版本是一个隐含的标准,但不幸的是,我无法更改 Weblogic 的配置......所以这意味着我们最终会得到两个不同的 spring 配置文件(我们使用的是 spring 2.5)来促进不同的环境。

Is there an elegant way to address this. Can I look JNDI names up directly in Tomcat? Can Spring take a name and look in both places? Google searches or suggestions would be great.

有没有一种优雅的方法来解决这个问题。我可以直接在 Tomcat 中查找 JNDI 名称吗?Spring 可以取一个名字并同时查看两个地方吗?谷歌搜索或建议会很棒。

采纳答案by martsraits

JndiLocatorSupporthas a property resourceRef. When setting this true, "java:comp/env/" prefix will be prepended automatically. So I believe it would be correct to differentiate this parameter when moving from Tomcat to Weblogic.

JndiLocatorSupport有一个属性resourceRef。设置为 true 时,将自动添加“java:comp/env/”前缀。所以我相信在从 Tomcat 转移到 Weblogic 时区分这个参数是正确的。

回答by Marcio Aguiar

How about an evironment variable? Set developers machines with the tomcat name and production with the Weblogic name. You can even set your code to use a default one (WebLogic) in case the variable don't exist.

环境变量呢?使用 tomcat 名称设置开发者机器,使用 Weblogic 名称设置生产机器。您甚至可以将代码设置为使用默认代码 (WebLogic),以防该变量不存在。

回答by Binil Thomas

My application also had a similar problem and this is how I solved it:

我的应用程序也有类似的问题,这就是我解决它的方法:

1) WEB-INF/classes/application.propertiescontains the entry:

1)WEB-INF/classes/application.properties包含条目:

ds.jndi=java:comp/env/jdbc/tcds

2) On the WLS machine, I have an entry in the /etc/sysenvfile:

2) 在 WLS 机器上,我在/etc/sysenv文件中有一个条目:

ds.jndi=wlsds

3) I configured spring to lookup the JNDI vis the property ${ds.jndi}, using a PropertyPlaceholderConfigurerbean with classpath:application.propertiesand file:/etc/sysenvas locations. I also set the ignoreResourceNotFoundto trueso that developers need not have /etc/sysenvon their machines.

3)我配置了 spring 来查找 JNDI vis 属性${ds.jndi},使用PropertyPlaceholderConfigurer带有classpath:application.propertiesfile:/etc/sysenv作为位置的bean 。我还设置了ignoreResourceNotFoundtrue使开发人员无须/etc/sysenv在他们的机器。

4) I run an integration test using Cargo+Jetty and I could not properly set up a JNDI environment there. So I have a fallback BasicDataSourceconfigured too using the defaultObjectproperty of JndiObjectFactoryBean.

4) 我使用 Cargo+Jetty 运行集成测试,但我无法在那里正确设置 JNDI 环境。所以我有一个备用的BasicDataSource配置也使用defaultObject的财产JndiObjectFactoryBean

回答by harmanjd

How are you referencing the resource in spring?

你如何在 spring 中引用资源?

This is what we have for tomcat:

这是我们为 tomcat 所拥有的:

context:

语境:

<Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="
JMS Connection Factory"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://localhost:61615" brokerName="StandaloneAc
tiveMQBroker"/>

spring:

春天:

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<jee:jndi-lookup jndi-name="jms/ConnectionFactory" id="connectionFactory" resource-ref="true"
                         expected-type="javax.jms.ConnectionFactory" lookup-on-startup="false"/>

The jee namespace comes from:

jee 命名空间来自:

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.0.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd

回答by duffymo

I've managed the trick with Tomcat and WebLogic using Spring. Hereis a description of how it worked for me.

我已经使用 Spring 通过 Tomcat 和 WebLogic 管理了这个技巧。 是它如何为我工作的描述。

回答by Leonel

How to use a single JNDI name in your web app

如何在 Web 应用程序中使用单个 JNDI 名称

I've struggled with this for a few months myself. The best solution is to make your application portable so you have the same JNDI name in both Tomcat and Weblogic.

我自己已经为此挣扎了几个月。最好的解决方案是使您的应用程序可移植,以便在 Tomcat 和 Weblogic 中具有相同的 JNDI 名称。

In order to do that, you change your web.xmland spring-beans.xmlto point to a single jndi name, and provide a mapping to each vendor specific jndi name.

为了做到这一点,您将您的web.xmland更改spring-beans.xml为指向单个 jndi 名称,并提供到每个供应商特定 jndi 名称的映射。

I've placed each file below.

我把每个文件放在下面。

You need:

你需要:

  • A <resource-ref />entry in web.xml for your app to use a single name
  • A file WEB-INF/weblogic.xmlto map your jndi name to the resource managed by WebLogic
  • A file META-INF/context.xmlto map your jndi name to the resource managed by Tomcat
    • This can be either in the Tomcat installation or in your app.
  • <resource-ref />web.xml 中的条目,供您的应用使用单一名称
  • WEB-INF/weblogic.xml将 jndi 名称映射到 WebLogic 管理的资源的文件
  • META-INF/context.xml将 jndi 名称映射到 Tomcat 管理的资源的 文件
    • 这可以在 Tomcat 安装中或在您的应用程序中。

As a general rule, prefer to have your jndi names in your app like jdbc/MyDataSourceand jms/ConnFactoryand avoid prefixing them with java:comp/env/.

作为一般规则,更喜欢在您的应用程序中使用您的 jndi 名称,例如jdbc/MyDataSourceandjms/ConnFactory并避免在它们前面加上java:comp/env/.

Also, data sources and connection factories are best managed by the container and used with JNDI. It's a common mistake to instantiate database connection pools in your application.

此外,数据源和连接工厂最好由容器管理并与 JNDI 一起使用。在应用程序中实例化数据库连接池是一个常见的错误

spring

春天

<?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:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

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

web.xml

网页.xml

<resource-ref>
    <description>My data source</description>
    <res-ref-name>jdbc/appds</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

weblogic.xml

weblogic.xml

<?xml version="1.0" encoding="UTF-8" ?>
<weblogic-web-app
    xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://xmlns.oracle.com/weblogic/weblogic-web-app http://http://www.oracle.com/technology/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd">

<resource-description>
    <jndi-name>appds</jndi-name>
    <res-ref-name>jdbc/appds</res-ref-name>
</resource-description>
</weblogic-web-app>

META-INF/context.xml(for Tomcat)

META-INF/context.xml(用于Tomcat)

<Context>
    <ResourceLink global="jdbc/appds" name="jdbc/appds" type="javax.sql.DataSource"/>
</Context>

回答by Konstantin

Setting up DataSource in the application itself is not that crazy :) I would say that is even mandatory if application is meant to be deployed on a grid. River, GigaSpaces, or similar.

在应用程序本身中设置 DataSource 并不是那么疯狂:) 如果应用程序打算部署在网格上,我会说这甚至是强制性的。River、GigaSpaces 或类似产品。

Note: I do not say connection settings have to be hardcoded inside of WAR, they need to be supplied at deployment time/runtime. This simplifies management of cloud instances since there is only on place to configure.

注意:我并不是说连接设置必须在 WAR 内部进行硬编码,它们需要在部署时/运行时提供。这简化了云实例的管理,因为只需在现场进行配置。

Configuring resources at the container makes sense only if multiple applications are deployed there and they can use shared resource.

只有在容器中部署了多个应用程序并且它们可以使用共享资源时,在容器中配置资源才有意义。

Again, in cloud type of deployments there is only one application per servlet container instance.

同样,在云类型的部署中,每个 servlet 容器实例只有一个应用程序。

回答by Val Blant

The following config works in Tomcat and Weblogic for me.

以下配置适用于我的 Tomcat 和 Weblogic。

In Spring:

在春天:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
   <!-- This will prepend 'java:comp/env/' for Tomcat, but still fall back to the short name for Weblogic -->
   <property name="resourceRef" value="true" /> 
   <property name="jndiName" value="jdbc/AgriShare" />
</bean>

In Weblogic Admin Console create a JDBC resource named jdbc/AgriShare. Under 'Targets', MAKE SURE YOU TARGET THE DATASOURCE TO THE SERVER YOU ARE DEPLOYING YOUR APP TO!. This particular point cost me some time just now...

在 Weblogic 管理控制台中,创建一个名为 的 JDBC 资源jdbc/AgriShare。在“目标”下,确保您将数据源定位到您要将应用程序部署到的服务器!. 这个特殊的点刚刚花了我一些时间......