`

200. Spring Boot JNDI:在Tomcat中怎么玩JNDI?

阅读更多
 

 

【视频&交流平台】

à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

历史相关文章:

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)

 

à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

 

微信公众号「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或者扫描以下二维码即可关注:

分享到:
评论

相关推荐

    javax.naming.NamingException: Cannot create resource instance

    javax.naming.NamingException: Cannot create resource instance类加载异常,希望可以帮助跟我一样错误的人。

    com.sun.jndi.providerutil.jar

    maven稀缺jar资源

    spring-framework-3.0.0.M4-with-docs

    org.springframework.context-3.0.0.M4.jar: 提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等 org.spring...

    spring-mock.jar

    org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework....

    hibernate 3.1+tomcat 5.5.x(配置jndi)

    hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...

    spring 3.0 jar 所有开发包及开发项目实例

    org.springframework.context-3.0.0.M4.jar: 提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等 org.spring...

    Tomcat6[1].0中JNDI的配置以及常见问题

    Tomcat6[1].0中JNDI的配置以及常见问题.doc

    import com.sun.jndi.ldap.ctl.VirtualListViewControl;

    import com.sun.jndi.ldap.ctl.VirtualListViewControl;

    Tomcat6+spring+jndi配置数据源说明.docx

    Tomcat6+spring+jndi配置数据源说明.docx

    spring-boot-reference.pdf

    Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...

    Tomcat中JNDI原理

    简单我tomcat5.0中的JNDI应用

    揭开J2EE集群的面纱 pdf

    目录 1.前言 2.基本术语 2.1.可扩展性 2.2.高可用性 2.3.负载均衡 2.4.容错 2.5.失败转移 2.6.幂等方法 ...3.什么是J2EE集群?...4.2.3.Tomcat的方案:多服务器复制 4.2.4.WebLogic, WebSphere, ...11.附录A:中英文对照表

    hibernate中jndi的配置使用

    配置了tomcat之后发现jndi好简单啊,可是碰到了hibernate该怎么做呢,本例详细解析

    Spring Boot中文文档.rar

    spring boot中文文档,从安装到部署。 I. Spring Boot文件 1.关于文档 2.获得帮助 3.第一步 4.使用Spring Boot 5.了解Spring Boot功能 6.转向生产 7.高级主题 II。入门 8.介绍Spring Boot ...

    spring-aop-5.1.0.RELEASE.jar

    ### 在libs目录中,有四个Spring的基础包 spring-**core**-4.3.6.RELEASE.jar :包含spring框架基本的核心工具类,spring其他组件都要用到这个包里的类,其他组件的基本核心 spring-**beans**-4.3.6.RELEASE.jar:...

    weblogic spring jndi配置

    NULL 博文链接:https://zzy603.iteye.com/blog/1039826

    spring-jndi-lookup:如何使用Spring从JNDI查找数据源

    如何使用Spring从JNDI查找数据源 Server.xml &lt;资源名称=“ jdbc / javatechie”全局=“ jdbc / javatechie” auth =“容器” type =“ javax.sql.DataSource” driverClassName =“ com.mysql.jdbc.Driver” url...

    springboot参考指南

    在Spring环境中使用YAML暴露属性 iii. 23.6.3. Multi-profile YAML文档 iv. 23.6.4. YAML缺点 vii. 23.7. 类型安全的配置属性 i. 23.7.1. 第三方配置 ii. 23.7.2. 松散的绑定(Relaxed binding) iii. 23.7.3. @...

    com.sun.jndi.ldap.jar

    maven稀缺jar资源

    Windows_7_下搭建LDAP服务器并使用JNDI

    Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI

Global site tag (gtag.js) - Google Analytics