Spring's support for JNDI

Spring support for JNDI 


The access to JNDI in Spring provides convenient methods, and all classes are included in Spring's org.springframework.jndi package. It provides the following core classes: 

1) JndiTemplate : It is the core class of this package and is used to simplify the operation of JNDI. It provides methods for lookup and bind. 
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
    <property name="environment"> 
        <props> 
      <prop key="java.naming.factory.initial">org.jnp.interfaces. NamingContextFactory</prop> 
            <prop key="java.naming.provider.url">jnp://localhost:1099</prop> 
            <prop key="java.naming.factory.url.pkgs">org.jboss. naming:org.jnp.interfaces</prop> 
        </props> 
    </property> 




:looks up a JNDI object that exposes objects found in JNDI to other Bean references, such as using JndiObjectFactoryBean as  the "dataSource" property of a data access object in the case of a data source  , e.g.
<bean id="dataSource" class="org. springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName"> 
        <value>java:/MySqlDS</value> 
    </property> 
</bean> 

Instance: 

data source configuration: 

(1) The configuration can access the same application Server's jndi data source 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName"> 
        <value>jdbc/cqccms</value> 
    </property> 
</bean> 

(2) Configure access to remote jndi data source 
Method 1: Define remote JNDI parameters directly through the jndiEnvironment property of JndiObjectFactoryBean 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
  <property name="jndiName"> 
     <value>jdbc/cqccms</value> 
  </property> 
  <property name="jndiEnvironment"> 
     <props> 
        <prop key="java.naming.factory.initial"> 
          weblogic.jndi.WLInitialContextFactory 
        </prop> 
        <prop key="java.naming.provider.url">t3://172.16.101.42:7001</prop> 
        <prop key="java.naming.security.principal">weblogic</prop> 
        <prop key="java.naming.security.credentials">weblogic</prop> 
     </props>    
   </property> 
</bean> 

方法2:先定义jndiTemplate,由jndiTemplate定义远程JNDI参数 

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
    <property name="environment"> 
        <props> 
            <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> 
            <prop key="java.naming.provider.url">t3://172.16.101.42:7001</prop> 
            <prop key="java.naming.security.principal">weblogic</prop> 
            <prop key="java.naming.security.credentials">weblogic</prop> 
        </props> 
    </property> 
</bean> 

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
  <property name="jndiName"> 
     <value>jdbc/cqccms</value> 
  </property> 
  <property name="jndiTemplate" ref="jndiTemplate" /> 
</bean> 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326934429&siteId=291194637