what is java:comp/env?

后端 未结 2 484
孤城傲影
孤城傲影 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: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.

提交回复
热议问题