You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot与WildFly 11.0中配置JNDI数据源

Spring Boot + WildFly 11 配置PostgreSQL JNDI数据源指南

我来帮你一步步搞定这个配置,分两部分对应你的问题:

a. WildFly 11 standalone.xml 配置步骤

首先得把PostgreSQL驱动部署到WildFly里,不然WildFly找不到驱动类:

  1. 创建PostgreSQL驱动模块
    在WildFly安装目录下,新建路径:modules/system/layers/base/org/postgresql/main
    把PostgreSQL的驱动jar包(比如postgresql-42.6.0.jar)放到这个目录,再新建一个module.xml文件,内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.3" name="org.postgresql">
        <resources>
            <resource-root path="postgresql-42.6.0.jar"/>
        </resources>
        <dependencies>
            <module name="javax.api"/>
            <module name="javax.transaction.api"/>
        </dependencies>
    </module>
    

    记得把path里的jar包名称换成你实际用的版本。

  2. 修改standalone.xml 添加数据源
    打开WildFly的standalone/configuration/standalone.xml,找到<datasources>节点,添加你的PostgreSQL数据源配置:

    <datasource jndi-name="java:/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">
        <connection-url>jdbc:postgresql://localhost:5432/your_database_name</connection-url>
        <driver>postgresql</driver>
        <security>
            <user-name>your_db_username</user-name>
            <password>your_db_password</password>
        </security>
        <validation>
            <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
            <validate-on-match>true</validate-on-match>
            <background-validation>false</background-validation>
        </validation>
    </datasource>
    

    接着在<drivers>节点下添加PostgreSQL驱动的引用:

    <driver name="postgresql" module="org.postgresql">
        <driver-class>org.postgresql.Driver</driver-class>
        <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
    </driver>
    

    这里的jndi-name要记好,后面Spring Boot配置会用到。

b. Spring Boot 主应用类与配置修改

  1. 修改application.properties
    把原来直接配置数据源的内容(比如spring.datasource.url、spring.datasource.username)替换成JNDI引用:

    # 引用WildFly里配置的JNDI数据源
    spring.datasource.jndi-name=java:/PostgresDS
    
    # JPA相关配置(按需调整)
    spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    
  2. 主应用类ProjectPocApiApplication 修改
    如果是把Spring Boot打包成war包部署到WildFly,需要让主类继承SpringBootServletInitializer,这样WildFly能正确识别并启动Spring应用:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class ProjectPocApiApplication extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(ProjectPocApiApplication.class, args);
        }
    
        // 重写configure方法,让WildFly加载Spring应用上下文
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(ProjectPocApiApplication.class);
        }
    }
    

    要是之前有自动配置嵌入式数据源的情况,可以在@SpringBootApplication里排除自动配置类:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}),不过一般来说,只要配置了spring.datasource.jndi-name,Spring Boot会自动优先使用JNDI数据源,不用额外排除。

最后补充个打包注意点:要把项目打包成war包,在pom.xml里设置<packaging>war</packaging>,并排除Spring Boot的嵌入式Tomcat依赖:

<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>
<!-- 添加Servlet API依赖,适配WildFly的Servlet环境 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

内容的提问来源于stack exchange,提问作者samdset

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:33:59