【视频&交流平台】

à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」或者扫描以下二维码即可关注:

201. Spring Boot JNDI:Spring Boot中怎么玩JNDI的更多相关文章

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

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

  2. Spring Boot:在Spring Boot中使用Mysql和JPA

    本文向你展示如何在Spring Boot的Web应用中使用Mysq数据库,也充分展示Spring Boot的优势(尽可能少的代码和配置).数据访问层我们将使用Spring Data JPA和Hiber ...

  3. 204. jetcache:在Spring Boot中怎么玩?

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

  4. 【spring Boot】Spring中@Controller和@RestController之间的区别

    spring Boot入手的第一天,看到例子中的@RestController ............. 相同点:都是用来表示Spring某个类的是否可以接收HTTP请求 不同点:@Controll ...

  5. 漫谈Spring Security 在Spring Boot 2.x endpoints中的应用(一)

    Spring Boot 2.x极大简化了默认的安全配置,并不是说有很多安全相关的配置,现在你只需要提供一个WebSecurityConfigurerAdapter继承类这样一个简单的操作,Spring ...

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

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

  7. Spring Boot(2)中的yaml配置简介

    搞Spring Boot的小伙伴都知道,Spring Boot中的配置文件有两种格式,properties或者yaml,一般情况下,两者可以随意使用,选择自己顺手的就行了,那么这两者完全一样吗?肯定不 ...

  8. spring mvc 和spring boot 中注解的使用

    1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...

  9. spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。

    需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@  而不是  ...

随机推荐

  1. reduction_indices in tensorflow

    https://www.cnblogs.com/likethanlove/p/6547405.html

  2. Eclipse ee项目 Java Resources文件报错解决方法

    通常是JDK版本配置问题,这里使用的是JDK1.8版本 确保jre版本为本机配置的版本 打开Navigator目录的.settings文件夹修改org.eclipse.wst.common.compo ...

  3. Java中主类中定义方法加static和不加static的区别

     Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用(类名.方法),后者必须先实例化后用实例调用) 知识点:1.Getter and Setter 的应用 ...

  4. Bitmap RGB24 4字节对齐

    Bitmap RGB24 4字节对齐 本文中说的图片都是无压缩的彩色Bitmap图片. 最近在一个项目中有一个场景是需要将RGB32或RGB24的Bitmap转换成为RGB565的Bitmap,在RG ...

  5. 定义action的允许访问方式

    publicfunction behaviors() { return[ 'verbs'=>[ 'class'=>VerbFilter::className(), 'actions'=&g ...

  6. sklearn.preprocessing.LabelBinarizer

    sklearn.preprocessing.LabelBinarizer

  7. Cortex-M3的一些概念

    [工作模式] 线程模式(Thread mode):处理器复位或异常退出时为此模式.此模式下的代码可以是特权代码也可以是用户代码,通过CONTROL[0]控制.处理模式(Handler mode):出现 ...

  8. sql server中用聚合函数查询退休人的开销信息

    1创建表 create database Mathgouse MathgoCREATE TABLE A(ID INT PRIMARY KEY IDENTITY,--自增主键ID_CARD VARCHA ...

  9. 学习笔记TF046:TensoFlow开发环境,Mac、Ubuntu/Linux、Windows,CPU版本、GPU版本

    下载TensorFlow https://github.com/tensorflow/tensorflow/tree/v1.1.0 .Tags选择版本,下载解压. pip安装.pip,Python包管 ...

  10. LeetCode - Fruit Into Baskets

    In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...