How does an application that uses Spring's SimpleNamingContextBuilder
as its JNDI provider know to search its directory for resources? What links the application to the Spring naming directory? For example, how does the JndiObjectFactoryBean
bean in this earlier answer know to find resource my-db
in the Spring directory? Doesn't JndiObjectFactoryBean
require a context environment with property java.naming.factory.initial
set to some implementation of interface InitialContextFactory
? What should the value of java.naming.factory.initial
be when using SimpleNamingContextBuilder
as the JNDI provider?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Java runtime class
NamingManager
serves as the link between a Java application and its naming directory. When aSimpleNamingContextBuilder
activates, it installs itself to static memberInitialContextFactoryBuilder
inNamingManager
. When the application creates anInitialContext
to retrieve the JNDI context, classInitialContext
delegates to NamingManager, which in turn asks theIntialContextFactoryBuilder
(in this case,SimpleNamingContextBuilder
) to create anIntialContextFactory
, which ultimately creates theInitialContext
.JndiObjectFactoryBean
doesn't need an explicit context environment becauseSimpleNamingContextBuilder
provides theInitialContextFactory
to theNamingManager
andJndiObjectFactoryBean
uses theNamingManager
to retrieve its resources. So, in the earlier answer,JndiObjectFactoryBean
"knows" to search the Spring naming directory for resourcemy-db
becauseSimpleNamingContextBuilder
has established itself as the JNDI provider in theNamingManager
.In a nutshell, If want to mock JNDI tree with mock InitialContext in unit tests , SimpleNamingContextBuilder can be used. I instantiated SimpleNamingContextBuildeit in a startup method of test and successfully creates a in-memory InitialContext. e.g. in a spring test class..