標籤:javaee
JavaEE中資源或組件是如何通過JNDI尋找的一直都沒完全弄清楚,這段時間花時間總算把它弄清楚了,總結如下:
(a).JavaEE1.5以前:
ServletA.java:
InitialContext ic=new InitialContext();DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/FooDS");web.xml:
<resource-ref> <res-ref-name>jdbc/FooDS</res-ref-name> <res-type>javax.sql.DataSource</res-type></resource-ref>
sun-web.xml:
<resource-ref> <res-ref-name>jdbc/FooDS</res-ref-name> <jndi-name>jdbc/OracleDS</jndi-name></resource-ref>
其中sun-web.xml是廠商特定的設定檔,不是JavaEE規範要求的,主要的作用是將容器中需要用到的資源或組件與應用伺服器定義的資源或組件關聯,這樣的設定檔包括:
GlassFish&Sun AppServer: sun-web.xml sun-ejb-jar.xml sun-application-client.xml
<res-ref-name> is the name of a component dependency and is relative to java:comp/env
<jndi-name> is the name of a physical AppServer resource and is relative to the root of the AppServer‘s global namespace
(b).從JavaEE 5開始可以使用Annotations:
@Resource(name="jdbc/FooDS")private DataSource ds;
等同於web.xml或ejb-jar.xml檔案中的如下定義
<resource-ref> <res-ref-name>jdbc/FooDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <injection-target> <injection-target-class>com.acme.ServletA</injection-target-class> <injection-target-name>ds</injection-target-name> </injection-target></resource-ref>
其中name屬性的預設值為:
FIELD: <class-name>/<field-name>
METHOD: <class-name>/<setter-property-name>
TYPE: No defaulting. name() is required.
例如:
@Resource private DataSource ds;
等同於:
@Resource(name="com.acme.ServletA/ds")private DataSource ds;
(c).可以使用mapped-name直接關聯到在應用伺服器上註冊的資源或組件的全域名
// TYPE-level @Resource in Servlet
@Resource(name="jdbc/FooDS", type=javax.sql.DataSource.class, mappedName="jdbc/OracleDS")public class ServletA{ ... }等同於:
<resource-ref> <res-ref-name>jdbc/FooDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <mapped-name>jdbc/OracleDS</mapped-name></resource-ref>
使用這個功能的問題如下:
(1)不是JavaEE5規範要求必須實現的功能
(2)沒有統一的格式和文法,各廠商可以自己定義
(3)可以被優先順序更高的配置覆蓋,annotation < standard.xml < sun-*.xml
也可以使用mapped-name發布資源或組件在應用伺服器上的全域名
FooBean.java:
@Stateless(mappedName="ejb/MyFooBean")public class FooBean implements Foo{public String hello() { return "hello,world!/n"; }}MyAppClient.java:
@EJB(mappedName="ejb/MyFooBean")private static Foo foo;
(d).對於沒有使用mapped-name,同時也沒有在廠商特定的設定檔中設定關聯的資源或組件,應用伺服器採用如下規則:
(1).Session beans & ejb-refs
jndi-name 自動化佈建成 Home/Business介面名
(2)[email protected], resource-ref, message-destination-ref, etc.
jndi-name 自動化佈建成資源的 java:comp/env名
例如:
@Resource(name="jdbc/FooDS")private DataSource ds;
等同於sun-web.xml:
<resource-ref> <res-ref-name>jdbc/FooDS</res-ref-name> <jndi-name>jdbc/FooDS</jndi-name></resource-ref>
@Stateless(name="FooBean")public class FooBean implements Foo{ .. }等同於sun-ejb-jar.xml:
<ejb> <ejb-name>FooBean</ejb-name> <jndi-name>com.acme.Foo</jndi-name></ejb>
@EJB(name="ejb/Foo")private static Foo foo;
等同於sun-application-client.xml:
<ejb-ref> <ejb-ref-name>ejb/Foo</ejb-ref-name> <jndi-name>com.acme.Foo</jndi-name></ejb-ref>
(e).採用Global JNDI
java:global[/<app-name>]/<module-name>/<bean-name>!<fully-qualified-interface-name>
<bean-name> corresponds to the session bean‘s EJB name. It defaults to the unqualified name of the session bean class.
It can be explicitly specified using the name attribute of the @Stateless/@Stateful/@Singleton annotation.
Alternatively, if the ejb-jar.xml is being used to define the component, <bean-name> corresponds to the <ejb-name> element of ejb-jar.xml.
(f).如果同一個資源或組件用Annotation,javaEE規範定義的設定檔,廠商特定的設定檔中都定義了,那到底用哪個一般遵循如下優先順序:
Annotation < javaEE規範定義的設定檔 < 廠商特定的設定檔
例如glassfish中:
@Resouce < <resource-ref> in web.xml< <resource-ref> in sun-web.xml
glassfish, weblogic, websphere都遵循上面的順序,只不過廠商特定的設定檔不同而已。
參考文檔:
https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html
https://glassfish.java.net/javaee5/ejb/compdependencies_xmlforum_nov15.pdf
EJB和Web容器中的資源或組件是如何尋找的?