简体   繁体   English

使用@Resource和JNDI查找遇到问题

[英]Trouble using @Resource and JNDI lookup

I'm having trouble applying the concept of injection and also JNDI with EJBs and I'd like your help. 我在将注入的概念以及JNDI与EJB结合使用时遇到麻烦,希望得到您的帮助。 I am learning this and I really want to understand and apply the techniques of @Resource and/or JNDI lookups with XML configuration. 我正在学习这一点,我真的很想了解和应用XML配置的@Resource和/或JNDI查找技术。 I can't seem to find any of the initial parameters in my JNDI lookups. 我似乎在我的JNDI查找中找不到任何初始参数。 Now, before I continue, if I manually enter the JNDI name of my datasource, everything works great. 现在,在继续之前,如果手动输入数据源的JNDI名称,那么一切都会很好。 What I'm trying to do (again as an exercise) is use @Resource or JNDI to get the JNDI datasource name itself and then do a JNDI lookup on the datasource. 我想做的(再次作为练习)是使用@Resource或JNDI来获取JNDI数据源名称本身,然后在数据源上进行JNDI查找。 I know that you can directly inject a DataSource object but if I can't inject a String, I gotta start with this. 我知道您可以直接注入DataSource对象,但是如果我不能注入String,则必须从此开始。

First, here's the important part of my ejb-jar.xml: 首先,这是我的ejb-jar.xml的重要部分:

<env-entry>
    <description>DataSource JNDI lookup name</description>
    <env-entry-name>datasourceName</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>myDataSource</env-entry-value>
</env-entry>

In terms of injection using @Resource I've tried I've tried the following field in the EJB 在使用@Resource进行注入方面,我尝试了EJB中的以下字段

@Resource(name = "datasourceName")
String dsName;

I've also tried with mappedName and lookup (using JNDI lookup) nothing is coming in. Then I've tried JNDI lookup as follows: 我还尝试了mapdName和lookup(使用JNDI查找),什么也没有进来。然后,我尝试了JNDI查找,如下所示:

 // get JNDI name from environment entry in EJB Context and use to lookup DataSource
Context context = new InitialContext();
String dsName = (String) context.lookup("java:comp/env/SRS/datasourceName");

(The name of the application is SRS) - This comes up with a NamingException that the JNDI lookup did not find anything. (应用程序的名称是SRS)-这带有NamingException,即JNDI查找找不到任何东西。 I've tried the following lookups: 我尝试了以下查找:

String datasourceName = (String) context.lookup("java:comp/env/SRS/Status/datasourceName"); // name of EJB is Status
String datasourceName = (String) context.lookup("datasourceName");
String datasourceName = (String) context.lookup("SRS/datasourceName");           
String datasourceName = (String) context.lookup("SRS/Status/datasourceName");           
String datasourceName = (String) context.lookup("java:global/SRS/datasourceName");
String datasourceName = (String) context.lookup("java:global/SRS/Status/datasourceName");

My first post here so I hope I asked properly. 我在这里的第一篇文章,所以希望我能正确询问。 Thanks for the help! 谢谢您的帮助!

Ok. 好。 Two days later I saw that my mistake was nowhere in the code of the EJB itself but in a configuration of the JSP which invoked the EJB!! 两天后,我发现我的错误不在EJB本身的代码中,而是在调用EJB的JSP的配置中! My mistake is that I had somehow invoked the methods of the EJB class as a POJO and NOT through the container. 我的错误是我以某种方式将EJB类的方法作为POJO而不是通过容器来调用。 I had assumed since the EJB methods were being called that the container had correctly injected the EJB to the JSP. 我假设自从调用EJB方法以来,容器就已经将EJB正确地注入了JSP。 But it didn't. 但事实并非如此。 I put in a JNDI lookup in the JSP and everything resolved itself. 我在JSP中进行了JNDI查找,一切都解决了。 Thanks everyone for your responses! 感谢大家的回应!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM