如何使用Spring Boot& amp;配置JNDI数据源WildFly 11.0

时间:2018-05-02 23:42:46

标签: java spring-boot jboss wildfly jndi

我想用Spring Boot& amp;来配置JNDI数据源。 WildFly 11.0。我目前正在使用Postgres DB并在application.properties下拥有数据源配置。

我知道可以使用application.properties中的以下内容创建jndi。我在努力解决的问题是如何将其与JBoss WildFly联系起来?

一个。我需要在WildFly中的standalone.xml下添加/更改 湾Spring Boot主应用程序类ProjectPocApiApplication

下所需的任何更改

由于

ProjectPocApiApplication.java

@SpringBootApplication
public class ProjectPocApiApplication  extends SpringBootServletInitializer{

public static void main(String[] args) {

    SpringApplication.run(ProjectPocApiApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(ProjectPocApiApplication.class);

}}

的pom.xml

<?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>com.poc.project</groupId>
<artifactId>project-poc-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>project-poc-api</name>
<description>POC for Project Management Dashboard </description>

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
         </exclusions>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1206-jdbc42</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope>
     </dependency>
    </dependencies> 

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:3)

我认为您需要在独立xml的数据源子系统中进行以下类型的配置。 (确保在WildFly主目录下存在org.postgresql模块结构)

<subsystem xmlns="urn:jboss:domain:datasources:2.0">
    <datasources>
        <datasource jndi-name="java:jboss/datasources/PostgreSQLDataSource" pool-name="PostgreSQLDataSourcePool" enabled="true" use-java-context="true">
            <connection-url>jdbc:postgresql://localhost/test</connection-url>
            <driver>postgresql</driver>
        </datasource>
        <drivers>
            <driver name="postgresql" module="org.postgresql">
                <datasource-class>org.postgresql.Driver</datasource-class>
            </driver>
        </drivers>
    </datasources>
</subsystem>

我猜您可以使用以下方法在主类中配置数据源

@Bean
public DataSource dataSource() 
{
  JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
  DataSource dataSource = dataSourceLookup.getDataSource("java:jboss/datasources/PostgreSQLDataSource");
  return dataSource;
}

(没有真正测试过,只是扔掉它)