[英]Jndi Lookup in EJB and in its dependencies
I have a Java application deployed on Websphere 8.5 as an EAR. 我有一个作为EAR部署在Websphere 8.5上的Java应用程序。 I am trying to figure out how do a JNDI lookup from ejb-jar.xml.
我试图找出如何从ejb-jar.xml中进行JNDI查找。 (Noob to EJB) I typically do JNDI entries lookup from the WAR by adding the entries in web.xml.
(从EJB到Noob)我通常通过在web.xml中添加条目来从WAR中查找JNDI条目。 Now working on a ejb module I figured out I can still do JNDI lookup at ejb bean[1].
现在,我正在研究ejb模块,我仍然可以在ejb bean [1]上进行JNDI查找。 See below my ejb-jar.xml:
见下面我的ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?><ejb-jar id="ejb-jar_ID" version="2.1"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>MyService-ejb</display-name>
<enterprise-beans>
<message-driven id="MyServiceSyncOutboundCom">
<ejb-name>MyServiceSyncOutboundcom</ejb-name>
<ejb-class>
com.company.MyService.sync.com.MyServiceSyncOutboundCom
</ejb-class>
<transaction-type>Container</transaction-type>
<message-destination-type>
javax.jms.Queue
</message-destination-type>
<env-entry>
<env-entry-name>minBalance</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>500</env-entry-value>
</env-entry>
<env-entry>
<description></description>
<env-entry-name>maxCreditBalance</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>10000</env-entry-value>
</env-entry>
</message-driven>
</enterprise-beans>
My question: How do I get these entries into my class now that I have no web.xml ? 我的问题:既然没有web.xml,如何将这些条目添加到类中? Also if the ejb module comprises of a JAR dependency say
myservice-config.jar
with a Configs
class, can I retrieve these entries in that class instead of my Message Driven Bean? 另外,如果ejb模块包含JAR依赖项,例如带有
Configs
类的myservice-config.jar
,是否可以在该类中而不是我的消息驱动Bean中检索这些条目? How does that change the ejb-jar.xml
file then? 那如何改变
ejb-jar.xml
文件?
[1] https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/servjndi008.htm [1] https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/servjndi008.htm
The ejb-jar.xml
file serves the same purpose for EJBs as the web.xml
file does for servlets. ejb-jar.xml
文件对EJB的作用与web.xml
文件对servlet的作用相同。 The XML syntax for injecting into an EJB is the same as the XML syntax for injecting into a servlet, except in ejb-jar.xml
it is nested in an enterprise-bean, rather than just at the module level. 注入到EJB的XML语法与注入到servlet的XML语法相同,只是在
ejb-jar.xml
它嵌套在企业bean中,而不仅仅是在模块级别。 From your example, it would be something like this: 在您的示例中,将是这样的:
<env-entry>
<env-entry-name>minBalance</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>500</env-entry-value>
<injection-target>
<injection-target-class>com.company.MyService.sync.com.MyServiceSyncOutboundCom</injection-target-class>
<injection-target-name>name of field or method</injection-target-name>
</injection-target>
</env-entry>
You can only inject into classes that will be created by a container; 您只能注入将由容器创建的类; so for example, servlets are created by the Web Container;
因此,例如,servlet是由Web容器创建的; EJB instances and interceptors are created by the EJB Container.
EJB实例和拦截器由EJB容器创建。
Your Configs
class does not appear to be a class that is managed by the container, so cannot be the target of injection. 您的
Configs
类似乎不是由容器管理的类,因此不能成为注入的目标。 I can think of a couple of options to still inject into it: 我可以想到仍然可以采用的几种选择:
1 - In your ejb-jar.xml
, just declare it to be a singleton bean; 1-在您的
ejb-jar.xml
,只需将其声明为单例bean即可; then inject your env-entry
values into it. 然后将您
env-entry
值注入其中。 You could then inject the Configs
singleton bean into other beans. 然后,您可以将
Configs
单例bean注入其他bean。 Although perhaps non-standard, ejb-jar.xml
can declare any class on the application classpath to be an EJB. 尽管可能是非标准的,但
ejb-jar.xml
可以将应用程序类路径上的任何类声明为EJB。
2 - Declare Confg
to be a CDI managed bean and use the @Inject
annotation. 2-声明
Confg
为CDI托管bean,并使用@Inject
批注。 CDI does not have anything like web.xml
or ejb-jar.xml
, so you would have to use annotations when using CDI. CDI没有类似于
web.xml
或ejb-jar.xml
任何内容,因此在使用CDI时必须使用注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.