what is java:comp/env?

后端 未结 2 381
孤城傲影
孤城傲影 2020-12-07 08:47

what is meant by java:comp/env ?

What does the look up like :

Context envContext = (Context)initContext.lookup(\"java:comp/env\");


        
相关标签:
2条回答
  • 2020-12-07 09:12

    It's an in-memory global hashtable where you can store global variables by name.

    The "java:" url scheme causes JNDI to look for a javaURLContextFactory class, which is usually provided by your app container, e.g. here is Tomcat's implementation javadoc

    See also NamingManager.getURLContext

    0 讨论(0)
  • 2020-12-07 09:30

    java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB).

    Context envContext = (Context)initContext.lookup("java:comp/env");
    

    allows defining a variable pointing directly to this node. It allows doing

    SomeBean s = (SomeBean) envContext.lookup("ejb/someBean");
    DataSource ds = (DataSource) envContext.lookup("jdbc/dataSource");
    

    rather than

    SomeBean s = (SomeBean) initContext.lookup("java:comp/env/ejb/someBean");
    DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/dataSource");
    

    Relative paths instead of absolute paths. That's what it's used for.

    0 讨论(0)
提交回复
热议问题