如何从IBM WebSphere 9在Spring Boot中获取JNDI数据源 [英] How to get JNDI datasource in Spring Boot from IBM WebSphere 9

查看:317
本文介绍了如何从IBM WebSphere 9在Spring Boot中获取JNDI数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用jdbc作为SQL Server将SpringBoot应用程序部署到WebSphere 9. 我需要从Websphere设置(而不是从app.properties)获取jdbc用户名和密码.

Trying to deploy SpringBoot app to WebSphere 9 with using jdbc as SQL Server. I need to get jdbc username and password from websphere settings (not from app.properties).

据我了解,我可以使用InitialConfig.lookup()或JndiDataSourceLookup,但是如何找到数据库,用户名和密码?

As I understand, I can use InitialConfig.lookup() or JndiDataSourceLookup, but how could I find database, username and password?

推荐答案

选项1:

将以下属性添加到application.properties

spring.datasource.jndi-name=jdbc/jndiName

在Spring Boot主类或配置类中添加数据源bean定义

add datasource bean definition in spring boot main class or config class

 @Autowired
 private Environment env;

@Bean
public DataSource dataSource() throws NamingException {
    return (DataSource) new JndiTemplate()
                             .lookup(env.getProperty("spring.datasource.jndi-name"));
 }

选项2:

使用Java标准API

using Java standard API

DataSource ds = javax.naming.InitialContext.doLookup("jdbc/jndiName");

这篇关于如何从IBM WebSphere 9在Spring Boot中获取JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆