我想使用 Spring Boot 和 WildFly 11.0 配置 JNDI 数据源。我目前正在使用 Postgres DB,并且在 application.properties 下有数据源配置。
我知道可以使用 application.properties 中的以下内容创建 jndi。我所困扰的是如何将其与 JBoss WildFly 联系起来?
a.我需要在 WildFly 中的standalone.xml 下添加/更改什么 b. 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>
最佳答案
我假设您需要在独立 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;
}
(没有真正测试过,只是把它扔掉)
关于java - 如何使用 Spring Boot 和 WildFly 11.0 配置 JNDI 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50145002/