zoukankan      html  css  js  c++  java
  • 201. Spring Boot JNDI:Spring Boot中怎么玩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:这是虾米?

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

    说明:

    (1)Spring Boot 版本:2.0.2.RELEASE

    (2)Tomcat版本:8.0.28

     

    前言:

           在上一篇文章中花了不少时间介绍了Tomcat中怎么玩JNDI,来重点来了,在Spring Boot中是怎么玩的呢???

     

    一、Spring Boot老版本怎么玩?

           在比较老的Spring Boot中是怎么玩的,大体的思路是:

    (1)注入TomcatFactory工厂类,获取到上下文Context,往上下文中设置resource对象。

    (2)注入jndi DataSource。

           具体代码如下(手机端支持左右滑动):

        @Bean
       public TomcatEmbeddedServletContainerFactory tomcatFactory() {
           return new TomcatEmbeddedServletContainerFactory() {
               @Override
               protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                       Tomcat tomcat)
    {
                   tomcat.enableNaming();
                   return super.getTomcatEmbeddedServletContainer(tomcat);
               }
               @Override
               protected void postProcessContext(Context context) {
                   ContextResource resource = new ContextResource();
                   resource.setName("jdbc/mydb");
                   resource.setType(DataSource.class.getName());
                   resource.setProperty("driverClassName", "com.mysql.jdbc.Driver");
                   resource.setProperty("url", "jdbc:mysql://localhost:3306/mydb");
                   resource.setProperty("username", "root");
                   resource.setProperty("password","root");
                   context.getNamingResources().addResource(resource);
               }
           };
       }
       @Bean
       public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
           JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
           bean.setJndiName("java:comp/env/jdbc/mydb");
           bean.setProxyInterface(DataSource.class);
           bean.setLookupOnStartup(false);
           bean.afterPropertiesSet();
           return (DataSource)bean.getObject();
       }

    二、Spring Boot 2.0版本怎么玩呢?

    2.1 打包成war包

           通过上一篇文章,我们可以把配置放到tomcat/conf/context.xml里,那么在Spring Boot中,我们只要配置jndi指向的名称就可以了,对于这个点的,Spring Boot还是提供了相应的配置的,在application.properties添加如下配置:

    spring.datasource.jndi-name=jdbc/mydb

    或者是:

    spring.datasource.jndi-name=java:comp/env/jdbc/mydb

           对于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>

           然后打包成war包,在tomcat容器中进行运行,但我们不能每次都打包去测试,这样肯定会影响开发效率的,那么对于Embedded Tomcat的话,要怎么搞呢?

     

    2.2 Embedded Tomcat

           对于Embedded Tomcat的话,需要添加ServletWebServerFactory进行配置:

    @Bean
       public ServletWebServerFactory servletContainer() {
           TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
               @Override
               protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                   tomcat.enableNaming();
                   return super.getTomcatWebServer(tomcat);
               }
               @Override
               protected void postProcessContext(Context context) {
                   ContextResource resource = new ContextResource();
                   resource.setName("jdbc/mydb");
                   resource.setType(DataSource.class.getName());
                   resource.setProperty("driverClassName", "com.mysql.jdbc.Driver");
                   resource.setProperty("url", "jdbc:mysql://localhost:3306/mydb");
                   resource.setProperty("username", "root");
                   resource.setProperty("password","root");
                   context.getNamingResources().addResource(resource);
                   super.postProcessContext(context);
               }
           };
           return tomcat;
       }

    说明:

    (1)对于SpringBoot 2.0是ServletWebServerFactory,旧一点的版本应该是TomcatEmbeddedServletContainerFactory,不然就会出现类无法找到了。

    (2)tomcat.enableNaming():启用默认禁用的JNDI命名

    (3)ContextResource:构建一个ContextResource对象,然后添加到Context对象中。

     

    在application.properties添加如下配置:

    spring.datasource.jndi-name=jdbc/mydb

    到这里就可以使用jndi构建的DataSource了。

     

    2.3 Embedded Tomcat+context.xml尝试

           我们知道对于context.xml有一种局部配置的方式是放到/META-INF/context.xml下的,那么对于Spring Boot目前支持这种方式嘛,经测试结果不支持,会报如下的错误:

    Failed to look up JNDIDataSource with name 'jdbc/mydb'; nested exception isjavax.naming.NoInitialContextException: Need to specify class name inenvironment or system property, or as an applet parameter, or in an applicationresource file: java.naming.factory.initial

           不支持的话,目前也只能等官方进行升级了,或者聪明的你还有别的方式?

     

    à悟空学员:http://t.cn/Rg3ICnJ

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

    开学季所有课程优惠一周

    下雨天,适合学「Spring Boot」

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

     

    Java8新特性:方法引用
    209. SpringBoot quartz:sqlserver启动只有 DECLARE CURSOR 才允许使用...
    风口之上,我是那头猪嘛?
    Java8新特性:Lambda表达式: 摸摸里面
    Java8新特性:Lambda表达式:过关斩将:使用场景
    Java8新特性:Lambda表达式:小试牛刀
    下雨天,适合学「Spring Boot」
    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中怎么玩?
    

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

  • 相关阅读:
    题解 [BZOJ1295][SCOI2009] 最长距离
    题解 [51nod1274] 最长递增路径
    #leetcode刷题之路21-合并两个有序链表
    #leetcode刷题之路20-有效的括号
    #leetcode刷题之路19-删除链表的倒数第N个节点
    #leetcode刷题之路18-四数之和
    #leetcode刷题之路17-电话号码的字母组合
    #leetcode刷题之路16-最接近的三数之和
    #leetcode刷题之路15-三数之和
    #leetcode刷题之路14-最长公共前缀
  • 原文地址:https://www.cnblogs.com/springboot-wuqian/p/9540439.html
Copyright © 2011-2022 走看看