带有JNDI数据源的Spring Boot [英] Spring Boot with JNDI Data Source

查看:143
本文介绍了带有JNDI数据源的Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新的Spring Boot Web应用程序,我想连接到JNDI数据源(在Tomcat的context.xml中定义的MySQL数据库).

I have a new Spring Boot web application that I want to connect to a JNDI data source (a MySQL database defined in Tomcat's context.xml).

但是,当我尝试这样做时,总是会遇到以下异常;

However when I attempt this, I always get the following exception;

org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE. If you want an embedded database please put a supported on on the classpath.

尽管我的pom.xml包含MySQL连接器

This is despite my pom.xml containing the MySQL connector

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.test</groupId>
<artifactId>twojndi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Two JNDI Data Sources</name>
<description>Two JNDI Data Sources Example</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </exclusion>
            <exclusion>
                <artifactId>tomcat-jdbc</artifactId>
                <groupId>org.apache.tomcat</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>org.test.twojndi.Application</start-class>
    <java.version>1.7</java.version>
</properties>

<build>
    <finalName>${artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

我已经定义了我的application.properties如下,以使用jndi-name属性.

I have defined my application.properties as follows to use the jndi-name property.

spring.datasource.jndi-name=java:comp/env/jdbc/twojndi_ds1
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

尽管如此,Spring似乎还是应该使用内存数据库.

However despite that it seems Spring believes that an in-memory database should be used.

如果我将application.properties定义为这样,我就可以连接到MySQL数据库

I am able to connect to the MySQL database if I define my application.properties as so

spring.datasource.url=jdbc:mysql://localhost:3306/twojndi_ds1
spring.datasource.username=twojndi
spring.datasource.password=twojndi
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

有人可以帮助我通过Spring Boot连接到JNDI吗?

Can anyone help me connect to JNDI with Spring Boot?

推荐答案

正如M. Deinum所评论的那样,JDNI查找是在Spring Boot 1.2中实现的,当前版本是1.2.0.M2.

As commented by M. Deinum, JDNI lookup is implemented in Spring Boot 1.2, current version is 1.2.0.M2.

如果要使用Spring Boot 1.1进行操作,则可以这样定义一个bean:

If you want to do it with Spring Boot 1.1, you can define a bean like this:

@Bean
public DataSource dataSource() {
    JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("jdbc/jndidatasource");
    try {
        jndiObjectFactoryBean.afterPropertiesSet();
    } catch (NamingException e) {
        LOGGER.error("Error while retrieving datasource with JNDI name jdbc/jndidatasource", e);
    }
    return (DataSource) jndiObjectFactoryBean.getObject();
}

这篇关于带有JNDI数据源的Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆