Understanding the practical use of InitialContext in java?

I worked most on old projects, where I often ctx.lookup("datasource"); across this line ctx.lookup("datasource"); many times. Depending on the use that I came across with the original context, it is used to get the Java object associated with some name in webserver / appserver.

  • For example, we create a datasource through the weblogic admin console, then we can use this programmaticaly object in a java program using ctx.lookup ("datasource"). If I remember correctly, I saw such code during the implementation of EJB and somewhere where some object was attached with some name in the server itself.

Java docs says When the initial context is constructed, its environment is initialized with the properties defined in the environment parameter passed to the constructor. Therefore, it is likely that the use that I mentioned earlier, the initial context is built with environmental parameters (which probably means objects created by the administrator on the server, for example, a data source, connection pool, if any). This is the only thing I could relate to the original context.

Please tell me if it is correct and correct to use the initial context class?

Basically, with the initial context, we can associate / search for a Java object with the same name. In the event that webserver / appserver is likely to have objects such as a datasource, the connection pool gets bound to the server at startup, and can we search for them right away?

+4
source share
1 answer

This is like using the context class correctly. In newer EJB implementations, you can also use @EJB and @PersistenceContext . For a deeper understanding, read the wikipedia article on Injection Dependency .

+2
source

Source: https://habr.com/ru/post/1400818/


All Articles