How does an application using Spring SimpleNamingContextBuilder know how to look for its directory for resources?

How does an application that uses Spring SimpleNamingContextBuilder as JNDI know how to search for resources in its directory? What links an application to a Spring name directory? For example, how JndiObjectFactoryBean bean in the previous answer know to find the my-db resource in the Spring directory? Does JndiObjectFactoryBean require a context environment with the java.naming.factory.initial property java.naming.factory.initial set some implementation of the InitialContextFactory interface? What should be the value of java.naming.factory.initial when using SimpleNamingContextBuilder as a JNDI provider?

+6
java spring jndi
source share
2 answers

The Java NamingManager execution NamingManager serves as a bridge between a Java application and its name directory. When a SimpleNamingContextBuilder activated , it sets itself to a static member of the InitialContextFactoryBuilder in the NamingManager . When the application creates an InitialContext to retrieve the JNDI context, the InitialContext class delegates to the NamingManager, which in turn asks for the IntialContextFactoryBuilder (in this case, SimpleNamingContextBuilder ) to create the IntialContextFactory , which ultimately creates the InitialContext .

JndiObjectFactoryBean does not require an explicit contextual environment because SimpleNamingContextBuilder provides an InitialContextFactory for the NamingManager and JndiObjectFactoryBean uses the NamingManager to retrieve its resources. Thus, in an earlier answer, JndiObjectFactoryBean “knows” to search the Spring name directory for the my-db resource, because SimpleNamingContextBuilder established itself as a JNDI provider in the NamingManager .

+8
source share

In a nutshell. If you want to mock the JNDI tree using mock InitialContext in unit tests, you can use SimpleNamingContextBuilder. I created an instance of SimpleNamingContextBuildeit in the launch method for testing and successfully created the source code in memory. for example in the spring testing class ..

 @BeforeClass public static void setupJndi() throws Exception { SimpleNamingContextBuilder.emptyActivatedContextBuilder(); Context context = new InitialContext(); context.bind("java:comp/env/jms/ConnectionFactory",myJmsConnectionFactory); } 
+13
source share

All Articles