【视频&交流平台】

à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配置文件中加入(代码支持左右滑动):

  1. <?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中加入资源引用(非必须的):

  1.    <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.xml的name值一致。

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

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

  1.    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>标签内,添加:

  1.   <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加入:

  1. <?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视频」最近更新:

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

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

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

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

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

200. Spring Boot JNDI:在Tomcat中怎么玩JNDI?的更多相关文章

  1. 201. Spring Boot JNDI:Spring Boot中怎么玩JNDI

      [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...

  2. spring boot项目发布tomcat容器(包含发布到tomcat6的方法)

    spring boot因为内嵌tomcat容器,所以可以通过打包为jar包的方法将项目发布,但是如何将spring boot项目打包成可发布到tomcat中的war包项目呢? 1. 既然需要打包成wa ...

  3. Spring boot 内置tomcat禁止不安全HTTP方法

    Spring boot 内置tomcat禁止不安全HTTP方法 在tomcat的web.xml中可以配置如下内容,让tomcat禁止不安全的HTTP方法 <security-constraint ...

  4. Spring Boot内嵌Tomcat session超时问题

    最近让Spring Boot内嵌Tomcat的session超时问题给坑了一把. 在应用中需要设置session超时时间,然后就习惯的在application.properties配置文件中设置如下, ...

  5. Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去

    1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...

  6. 如何优雅的关闭基于Spring Boot 内嵌 Tomcat 的 Web 应用

    背景 最近在搞云化项目的启动脚本,觉得以往kill方式关闭服务项目太粗暴了,这种kill关闭应用的方式会让当前应用将所有处理中的请求丢弃,响应失败.这种形式的响应失败在处理重要业务逻辑中是要极力避免的 ...

  7. Spring Boot内置Tomcat

    Spring Boot默认支持Tomcat/Jetty/Undertow作为底层容器.在之前实战相关的文章中,可以看到引入spring-boot-starter-web就默认使用tomcat容器,这是 ...

  8. Spring Boot 内嵌Tomcat的端口号的修改

    操作非常的简单,不过如果从来没有操作过,也是需要查找一下资料的,所以,在此我简单的记录一下自己的操作步骤以备后用! 1:我的Eclipse版本,不同的开发工具可能有所差异,不过大同小异 2:如何进入对 ...

  9. spring boot不要放在tomcat下启动,因为自身就带了集成tomcat

    spring boot不要放在tomcat下启动,因为自身就带了集成tomcat

随机推荐

  1. python 在.py文件中调用其他.py内的函数

      假设名为A.py的文件需要调用B.py文件内的C(x,y)函数 假如在同一目录下,则只需 import B if __name__ == "__main__": B.C(x,y ...

  2. oh_my_zsh

    oh_my_zsh  zsh默认已经安装   cat /etc/shells   查看安装没有   (https://xiaozhou.net/learn-the-command-line-iterm ...

  3. leetcode题解 5. Longest Palindromic Substring

    题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum l ...

  4. C# 读写西门子PLC数据,包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC

    本文将使用一个gitHub开源的组件技术来读写西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 官方 ...

  5. 如何在 Windows 中设置 /3GB 启动开关

    备注: 只有在下列操作系统中才支持 /3GB 开关: Windows 2000 Advanced Server Windows 2000 Datacenter Server Windows Serve ...

  6. Linux下初次使用github

    1.安装 1.1 使用yum安装的 命令:$ yum install git git-gui 1.2 生成密钥对,使用ssh-keygen方法 ssh-keygen -t [rsa|dsa],将会生成 ...

  7. Quartz动态修改数据库cronExpression(无须重启服务器即可更改定时时间)

    quartz通过动态设置配置文件确实可以实现与数据库的同步,但现实开发上线后我们基本是不会对配置文件等进行变动,因为重启一次服务器所需的成本太多. 这时,就需要我们仅仅修改数据库就能实现动态的更新定时 ...

  8. Python全栈之路----函数进阶----名称空间

    又名name space,顾名思义就是存放名字的地方,存什么名字呢?举例说明,若变量x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的=地方 名称空间共3种,分别如下 ...

  9. 解决插值表达式闪烁问题 - v-cloak

    v-cloak页面在js没有加载出来的时候,不显示该部分 linux可以通过响应式设计模式,来模拟2,3G网络 <!DOCTYPE html><html><head> ...

  10. css自定义checkbox和radio样式

    很常见的问题,也有许多人写过类似的文章,自己写来记录下 css代码如下: #myCheck + label,.myRadio + label{ width:16px; height:16px; bor ...