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

如何在Spring项目中配置DataSource?基于WildFly 10.1.0的配置求助

Configuring a DataSource for Spring on WildFly 10.1.0

Got it, let's walk through how to set up a DataSource for your Spring project running on WildFly 10.1.0. Since WildFly is a Java EE container, the best practice is to use a container-managed DataSource (via JNDI) — this lets WildFly handle connection pooling, lifecycle, and resource management, which is way more robust than managing it entirely in Spring.

Step 1: Configure the DataSource in WildFly

First, you need to define the DataSource in WildFly itself. You can do this either via the management console or by editing the standalone configuration file.

Option A: Edit standalone.xml (or domain.xml for domain mode)

Navigate to your WildFly installation's standalone/configuration folder and open standalone.xml. Find the <datasources> section and add your DataSource configuration:

<datasource jndi-name="java:/jdbc/MySpringDS" pool-name="MySpringDS" enabled="true" use-java-context="true">
    <!-- Replace with your database URL, user, and password -->
    <connection-url>jdbc:mysql://localhost:3306/your_database_name</connection-url>
    <driver-class>com.mysql.cj.jdbc.Driver</driver-class> <!-- Use the correct driver class for your DB -->
    <driver>mysql</driver> <!-- Match this to the driver name you'll define below -->
    <security>
        <user-name>db_username</user-name>
        <password>db_password</password>
    </security>
    <!-- Optional validation settings (example for MySQL) -->
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <validate-on-match>true</validate-on-match>
        <background-validation>false</background-validation>
    </validation>
</datasource>

Next, add the database driver definition in the <drivers> subsection of <datasources>:

<driver name="mysql" module="com.mysql">
    <driver-class>com.mysql.cj.jdbc.Driver</driver-class>
    <xa-datasource-class>com.mysql.cj.jdbc.MysqlXADataSource</xa-datasource-class>
</driver>

Option B: Add the Database Driver to WildFly's Modules

WildFly requires database drivers to be installed as modules. For MySQL (adjust for your DB):

  1. Create a folder structure: wildfly-10.1.0.Final/modules/system/layers/base/com/mysql/main
  2. Place your MySQL connector JAR (e.g., mysql-connector-java-8.0.33.jar) in this folder.
  3. Create a module.xml file in the same folder with this content:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-8.0.33.jar"/> <!-- Replace with your JAR name -->
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

Restart WildFly to apply these changes.

Step 2: Reference the JNDI DataSource in Spring

Now, connect your Spring project to the WildFly-managed DataSource. You can use either XML or Java configuration.

XML Configuration (e.g., applicationContext.xml)

Add a JNDI lookup bean, making sure to include the jee namespace:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/jee
           http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- Look up the WildFly DataSource via JNDI -->
    <jee:jndi-lookup id="dataSource" jndi-name="java:/jdbc/MySpringDS" resource-ref="true"/>

    <!-- Now you can use this dataSource bean in your Spring JDBC, Hibernate, etc. -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

Java Configuration (Annotation-Based)

Create a configuration class to define the DataSource bean via JNDI:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiObjectFactoryBean;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource() {
        JndiObjectFactoryBean jndiBean = new JndiObjectFactoryBean();
        jndiBean.setJndiName("java:/jdbc/MySpringDS");
        jndiBean.setResourceRef(true); // Ensures compatibility with Java EE resource references
        jndiBean.setProxyInterface(DataSource.class);
        jndiBean.afterPropertiesSet(); // Initialize the bean
        return (DataSource) jndiBean.getObject();
    }
}

If you really want to manage the DataSource directly in Spring (bypassing WildFly's pooling), you can use something like HikariCP. But note this misses out on WildFly's container-level management:

@Bean
public DataSource dataSource() {
    com.zaxxer.hikari.HikariDataSource hikariDS = new com.zaxxer.hikari.HikariDataSource();
    hikariDS.setJdbcUrl("jdbc:mysql://localhost:3306/your_database_name");
    hikariDS.setUsername("db_username");
    hikariDS.setPassword("db_password");
    hikariDS.setDriverClassName("com.mysql.cj.jdbc.Driver");
    return hikariDS;
}

Troubleshooting Tips

  • Double-check the JNDI name: WildFly uses java:/ for global JNDI resources. If you get a naming exception, verify the name matches exactly what's in standalone.xml.
  • Ensure the database driver module is correctly installed — WildFly won't find the driver if the module structure or module.xml is wrong.
  • If using a web app, you can add a <resource-ref> in web.xml to link the JNDI name to your app's local context, but this is optional if you use the global java:/ name.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:34:16