如何在Spring Boot中创建jdbc/dblogger的JNDI数据源并访问
解决方案:在Spring Boot中创建「jdbc/dblogger」JNDI数据源并与现有数据源共存
要在Spring Boot应用中创建指定名称的JNDI数据源,同时保留现有数据源Bean,需要完成以下步骤:
1. 开启嵌入式Tomcat的JNDI支持
Spring Boot默认关闭了嵌入式Tomcat的JNDI功能,需要通过自定义容器配置开启,并绑定数据源到指定的JNDI名称空间。
2. 编写JNDI数据源配置类
创建配置类,同时保留原有的数据源Bean:
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jndi.JndiObjectFactoryBean; import javax.sql.DataSource; import org.apache.tomcat.util.descriptor.web.ContextResource; @Configuration public class JndiDataSourceConfig { // 配置Tomcat开启JNDI并绑定「jdbc/dblogger」数据源 @Bean public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatJndiCustomizer() { return factory -> { factory.addContextCustomizers(context -> { ContextResource jndiResource = new ContextResource(); // 设置JNDI资源名称,对应lookup路径的「jdbc/dblogger」 jndiResource.setName("jdbc/dblogger"); jndiResource.setType(DataSource.class.getName()); // 配置数据库连接参数,根据你的实际环境调整 jndiResource.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver"); jndiResource.setProperty("url", "jdbc:mysql://localhost:3306/your_database"); jndiResource.setProperty("username", "your_username"); jndiResource.setProperty("password", "your_password"); // 配置连接池(以HikariCP为例,Spring Boot默认集成) jndiResource.setProperty("factory", "com.zaxxer.hikari.HikariJNDIFactory"); jndiResource.setProperty("maximumPoolSize", "10"); context.getNamingResources().addResource(jndiResource); }); }; } // 可选:将JNDI数据源注册为Spring Bean,方便Spring内部使用 @Bean(name = "jndiDbLoggerDataSource") public DataSource jndiDbLoggerDataSource() throws Exception { JndiObjectFactoryBean jndiBean = new JndiObjectFactoryBean(); jndiBean.setJndiName("java:/comp/env/jdbc/dblogger"); jndiBean.setResourceRef(true); jndiBean.afterPropertiesSet(); return (DataSource) jndiBean.getObject(); } // 保留原有数据源Bean @Bean @ConfigurationProperties(prefix = "datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } }
3. 关键注意事项
- JNDI名称匹配:自定义JAR中的lookup路径
java:/comp/env/jdbc/dblogger对应配置中ContextResource.setName("jdbc/dblogger"),必须严格匹配。 - 数据库驱动:确保
pom.xml中包含对应数据库的驱动依赖(以MySQL为例):<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> - 连接池配置:如果使用其他连接池,需要修改
factory属性为对应连接池的JNDI工厂类。 - Bean共存:原有
dataSourceBean与新增的JNDI数据源Bean名称不同,不会产生冲突;若需要其中一个作为默认数据源,可添加@Primary注解。
4. 测试验证
启动应用后,自定义JAR中的代码即可正常lookup到jdbc/dblogger数据源:
import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; // ... Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource ds = (DataSource) envContext.lookup("jdbc/dblogger"); // 验证数据源可用性 System.out.println(ds.getConnection());
内容的提问来源于stack exchange,提问作者World of Titans
相关产品推荐
相关产品推荐

