zoukankan      html  css  js  c++  java
  • 200. Spring Boot JNDI:在Tomcat中怎么玩JNDI?

     

     

    【视频&交流平台】

    à SpringBoot视频http://t.cn/R3QepWG

    à SpringCloud视频http://t.cn/R3QeRZc

    à Spring Boot源码https://gitee.com/happyangellxq520/spring-boot

    à Spring Boot交流平台http://412887952-qq-com.iteye.com/blog/2321532

    à Spring Boot Shiro视频http://t.cn/R3QDMbh

    à Spring Boot 2.0 之Spring Data 和JPAhttp://t.cn/R1pSojf

    历史相关文章:

    199. Spring Boot JNDI:这是虾米?

    前言:

           在上一节中,我们介绍了《Spring Boot JNDI:这是虾米?》,为了大家更好的理解什么是JNDI,这里就带大家一起看看,在Tomcat中是如何玩JNDI的。

           Tomcat配置JNDI有全局配置和局部配置。大致的有以下三种配置方式:

    (1)全局配置:基于context.xml的配置。

    (2)局部配置:基于server.xml的配置。

    (3)局部配置:基于META-INF 的配置。

     

    第一种:全局配置:基于context.xml的配置

    1)在tomcat的conf/context.xml配置文件中加入(代码支持左右滑动):

    <?xml version="1.0" encoding="UTF-8"?>
    <Context >  
    <Resource name="jdbc/mydb"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/mydb"
        username="root" password="root"
        maxActive="20" maxIdle="10"
        maxWait="10000"/>

    </Context>

    特别注意:如果是使用了eclipse进行开发测试的话,可能会碰到如下的异常信息:

    Cannot create JDBC driver of class '' for connect URL 'null'

          这是由于context.xml是在开发工具中的servers下的/context.xml,所以需要将配置信息配置servers/Tomcat/context.xml。

    2)在项目的web.xml中加入资源引用(非必须的):

        <resource-ref>   
           <description>DB Connection</description>  
           <res-ref-name>jdbc/mydb</res-ref-name>  
           <res-type>javax.sql.DataSource</res-type>  
           <res-auth>Container</res-auth>  
       </resource-ref>  

    其中res-ref-name值要和context.xmlname值一致。

    特别说明:这个配置是可有可无的,不配置这个的话,也是可以正常运行的。

    3)在jsp中调用加载jndi方式:

        Connection conn =null;
         try{
              //初始化查找命名空间
              Context ctx = new InitialContext();
              //InitialContext ctx = new InitialContext();亦可
              //找到DataSource,对名称进行定位java:comp/env是必须加的,后面跟你的DataSource名
              DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydb");
              //取出连接
               conn = ds.getConnection();
              String sql = "select *from test";
              PreparedStatement ps = conn.prepareStatement(sql);
              ResultSet rs = ps.executeQuery();
               while(rs.next()){
                    System.out.println("id:"+rs.getInt("id")+",name:"+rs.getString("name")+"");
               }
              out.println(conn);
              out.println(conn.isClosed());
              out.println("</br>");
               System.out.println("connection pool connected !!");  
         } catch (NamingException e) {
          System.out.println(e.getMessage());
         } catch (SQLException e) {
          e.printStackTrace();
         }finally{
              //注意不是关闭,是放回连接池.
              conn.close();
         }

    特别注意:不可以直接用main方法测试,必须通过启动容器从jsp中调用

    第二种:局部配置:基于server.xml的配置(不推荐使用)

    在tomcat的server.xml的<host>标签内,添加:

       <Context docBase="demo-jndi" path="/demo-jndi">
              <Resource name="jdbc/mydb"
                auth="Container"
                type="javax.sql.DataSource"
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/mydb"
                username="root" password="root"
                maxActive="20" maxIdle="10"
               maxWait="10000"/>

             </Context>

    其他配置同第一种方式。

    第三种:局部配置:基于META-INFO的配置

    在项目的META-INF 下面新建context.xml加入:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context >  
    <Resource name="jdbc/mydb"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/mydb"
        username="root" password="root"
        maxActive="20" maxIdle="10"
        maxWait="10000"/>

    </Context>

    其他配置同第一种方式。

     

    总结:

    如果要配置局部的话,推荐使用第三种方式,这样不依赖tomcat了。但是还是推荐使用第一种方式好,虽然依赖Tomcat,但是是全局的,而且可以配置多个。对于以后切换使用方便。另外在项目的web.xml中添加的资源引用可有可无。

    好了本篇对于Tomcat怎么玩JNDI的篇章就到此告一段落了,下一篇《SpringBoot中怎么JNDI – 还是有坑的》。

    「SpringCloud视频最近更新:

    24. 覆写Feign的默认配置Configuration之Contract
    25. Spring Cloud中关于Feign的常见问题总结
    26. 解决Spring Cloud中FeignRibbon第一次请求失败的方法
    27. Feign添加 fallbackFactory 属性来触发请求进行容灾降级
    28. 断路器Hystrix总结
    29. Health Indicator(健康指标) 和metrics stream(指标流)
    30. 断路器监控(Hystrix Dashboard)
    31. 断路器聚合监控(turbine)
    

     SpringCloud视频http://t.cn/R3QeRZc

    微信公众号「SpringBoot最近更新:

     

    Java8新特性:接口的默认方法
    208. Spring Boot Swagger2:排序 – 漂游记
    207. Spring Boot Swagger2:极简方式
    我读的书很多,但都没有你好看【一禅录】
    206. Spring Boot 2.0 Swagger2:使用
    205. Spring Boot 2.0 Swagger2:初识Swagger
    当要离开的时候,我却动情了
    205. jetcache:你需要知道的小技巧
    204. jetcache:在Spring Boot中怎么玩?
    遇见阿里,遇见自己
    203. 阿里jetcache
    202. 阿里Pandora Boot
    微信公众号赞赏功能升级了,真的假的?
    《喜剧之王》「我养你啊」之人生选择
    201. Spring Boot JNDI:Spring Boot中怎么玩JNDI
    510阿里日,马老师献上最走心、最科技范儿证婚词~
    200. Spring Boot JNDI:在Tomcat中怎么玩JNDI?
    199. Spring Boot JNDI:这是虾米?
    Spring Boot 数据库迁移系列
    Spring Boot葵花宝典:初露锋芒:MyBatis insert异常 Parameter 'name' not found
    198. Spring Boot Flyway工作原理
    

     搜索springboot或者扫描以下二维码即可关注:

  • 相关阅读:
    Jenkins安装和初始化配置
    KVM环境下vCPU绑定到物理CPU
    关于自动化测试环境的集成(Jenkins+RobotFramework+TestLink+SVN)
    Linux下PPPoE Server测试环境搭建
    CentOS安装使用vnc进行远程桌面登录
    linux下PPTP Server测试环境搭建
    202020211 20209301《Linux内核原理与分析》第一周作业
    博弈论学习笔记(一)四个入门结论
    官宣!Amazon EMR正式支持Apache Hudi
    LessCss学习笔记 CCH
  • 原文地址:https://www.cnblogs.com/springboot-wuqian/p/9418180.html
Copyright © 2011-2022 走看看