如何在Spring项目中配置DataSource?基于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):
- Create a folder structure:
wildfly-10.1.0.Final/modules/system/layers/base/com/mysql/main - Place your MySQL connector JAR (e.g.,
mysql-connector-java-8.0.33.jar) in this folder. - Create a
module.xmlfile 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(); } }
Optional: Spring-Managed DataSource (Not Recommended for WildFly)
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 instandalone.xml. - Ensure the database driver module is correctly installed — WildFly won't find the driver if the module structure or
module.xmlis wrong. - If using a web app, you can add a
<resource-ref>inweb.xmlto link the JNDI name to your app's local context, but this is optional if you use the globaljava:/name.
内容的提问来源于stack exchange,提问作者alexKruts

