zoukankan      html  css  js  c++  java
  • Springboot配置外部容器使用JNDI读取数据源

    Springboot配置外部容器使用JNDI读取数据源

    简介

    JNDI(Java Naming and Directory Interface,Java命名和目录接口)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访问提供者接口JNDI服务供应接口(SPI)的实现,由管理者将JNDI API映射为特定的命名服务和目录系统,使得Java应用程序可以和这些命名服务和目录服务之间进行交互。目录服务是命名服务的一种自然扩展。两者之间的关键差别是目录服务中对象不但可以有名称还可以有属性(例如,用户有email地址),而命名服务中对象没有属性 [1] 。(by百度百科)

    百度百科就是说的太专业了,简单来说就是将资源引入到服务器中通过JNDI提供给程序使用。

    需求

    将数据库连接配置改为读取JNDI数据源,由于公司用的是Resin环境下面示例我只在Resin上测试通过,其实换成Tomcat也是一样的原理。

    步骤

    • resin.xml配置

    将mysql-connector-java-5.0.8-bin.jar 放到resin的lib目录下

    <!-- the default host, matching any host name -->
    <host id="" root-directory=".">
        <database>
        		<jndi-name>jdbc/intf</jndi-name>
        		<driver type="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
        		  <url>jdbc:mysql://192.168.xxx.xxx:3306/intf</url>
        		  <user>root</user>
        		  <password>root</password>
        		</driver>
        		<prepared-statement-cache-size>8</prepared-statement-cache-size>
        		<max-connections>80</max-connections>
        		<max-idle-time>30s</max-idle-time>
        </database>
        <web-app id="/" root-directory="/data/web/all-adintf"/>
    </host>
    
    • 修改application.yml
    spring:
      # jndi-name 数据源 没见过这个属性吧我也是头一次见 (#^.^#)
      datasource:
        jndi-name: jdbc/intf
    
    • 创建DataSourceConfig
    package cn.pconline.config;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.jndi.JndiLocatorSupport;
    import org.springframework.jndi.JndiObjectFactoryBean;
    
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    /**
     * @Description 使用Resin配置的数据源
     * 1.配置Spring JNDI名称
     * 2.resin.xml
     * 3.注入dataSource Bean PS:Springboot就是实现零配置文件!! 所有的配置Bean的方法都是可以通过注解的方式注入
     * @Bean("dataSource") 如果使用Spring XML配置如下:
     * <jee:jndi-lookup id="dataSource" jndi-name="${sys.jndi.name}" expected-type="javax.sql.DataSource"/>
     *
     * 存在问题不能使用数据库连接池Alibaba Druid
     *
     * --- 备份 ---
     *  JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
     *  DataSource dataSource = dataSourceLookup.getDataSource(jndiName);
     *  System.out.println(dataSource);
     *  return dataSource;
     * @Author jie.zhao
     * @Date 2019/7/29 14:24
     */
    @Configuration
    @EnableConfigurationProperties
    public class DataSourceConfig {
    
        @Value("${spring.datasource.jndi-name}")
        private String jndiName;
    
        @Bean("dataSource")
        @Primary
        public DataSource primaryDataSource() throws NamingException {
            JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
            bean.setJndiName(JndiLocatorSupport.CONTAINER_PREFIX + jndiName);
            bean.setProxyInterface(DataSource.class);
            bean.setLookupOnStartup(false);
            bean.afterPropertiesSet();
            return (DataSource) bean.getObject();
        }
    }
    
    

    这样的的话我们就把数据源改为引用外部JNDI的了。

    存在的不足因为使用了外部容器启动,在开发时Springboot Main方法就不能用了,大大影响了开发调试的效率。

    Springboot解决Main方法启动无法注入JNDI

    -------------已经触及底线 感谢您的阅读-------------
  • 相关阅读:
    Chrome扩展开发之一——Chrome扩展的文件结构
    Chrome扩展开发(Gmail附件管理助手)系列之〇——概述
    Springfox与swagger的整合使用
    Maven的简单使用
    关于接口功能自动化的思考
    发版流程优化备忘录
    Aho-Corasick 自动机 学习笔记
    Luogu P1495 曹冲养猪
    Luogu P2670 【扫雷游戏】
    1.1 整除
  • 原文地址:https://www.cnblogs.com/cnsyear/p/12913329.html
Copyright © 2011-2022 走看看