没有java的Tomcat中的jndi资源:/ comp / env / prefix

时间:2015-11-12 05:27:23

标签: java tomcat7 websphere jndi spring-java-config

在Tomcat中,context.xml中定义的任何jndi资源都有前缀java:/comp/env/。在Websphere,他们不会。我们注入了相当多的资源

@Resource(mappedName="...")
private Queue queue1;

为本地和托管环境配置两次它是一件痛苦的事。 我想将它配置为只有@Resource注入,它将适用于所有环境。毕竟,这是使用jndi的重点。

有一些方法可以配置Tomcat来定义全局jndi资源,但它们都涉及更改server.xml,我们不能这样做,因为它是一个gradle tomcat插件。

有没有办法在不修改Tomcat中的server.xml的情况下实现相同的效果?

1 个答案:

答案 0 :(得分:1)

WebSphere ignores mappedName see here, so you can have there whatever you like for Tomcat, but for WebSphere use lookup=jndiName. The java:comp/env/ prefix is used for resource references, or resource definitions embedded in the application. It shouldn't be used as JNDI name for global objects defined in server configuration.

For WebSphere you can also use binding file (ibm-web-bnd.xml in the WEB-INF), and map there your reference to the JNDI name defined in the server configuration e.g.

// In code use just resource with name
@Resource(name="myQueue")    // this defines reference name which will be bound to jndi name using binding file
private Queue queue;


// binding file
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd 
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_2.xsd"
    version="1.2">

    <virtual-host name="default_host" />

    <resource-env-ref name="myQueue" binding-name="jms/myQ" />
</web-bnd>