spring配置JNDI(Java Naming and Directory Interface,Java命名和目录接口)数据源

1.在tomcat下的server.xml的 <GlobalNamingResources> </GlobalNamingResources>添加下面代码

    <Resource name="jdbc/mysql" auth="Container"   
                type="javax.sql.DataSource"  
                driverClassName="com.mysql.jdbc.Driver"  
                url="jdbc:mysql://127.0.0.1:3306/test"  
                username="root"  
                password="123456"  
                maxActive="50"  
                maxIdle="30"  
                maxWait="10000" />  

2.在于server.xml同目录的context.xml中的<context></context>添加下代码

    <ResourceLink name="jdbc/mysql" type="javax.sql.DataSource" global="jdbc/mysql"/> 

3.web.xml中可加也可不加下边的代码

  <resource-ref>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-sharing-scope>Unshareable</res-sharing-scope>
  </resource-ref>

4.spring的配置文件中添加下面代码

   <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName">
                  <value>java:comp/env/jdbc/mysql</value>
            </property>
      </bean>

5.测试,在jsp中测试

<% Context context = new InitialContext();
   DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/mysql");
   Connection connection = ds.getConnection();
   connection.close();
%>
原文地址:https://www.cnblogs.com/yxqing/p/10785771.html