【问题标题】:JNDI configuration with spring boot with WebSphere 9.0.0.7带有 WebSphere 9.0.0.7 的 Spring Boot 的 JNDI 配置
【发布时间】:2019-03-18 23:55:33
【问题描述】:
我添加了 spring.datasource.jndi-name=java:comp/env/jdbc/DB_Name
application.properties 文件中的属性,未创建数据源。
我想将该数据源与 spring boot 2.0.3、spring boot data jpa(hibernate)、websphere 一起使用。需要使用 jparepository 进行 CURD 操作。
我见过similar issue。但是我们在spring boot中没有web.xml来添加资源引用。所以不能按照这个答案。我怎样才能实现jparepository。
【问题讨论】:
标签:
java
spring-boot
websphere
jndi
ear
【解决方案1】:
需要在application.properties中添加如下属性
spring.datasource.jndi-name=jdbc/yourjndiname
在 SpringApplication.java 类中,我们的 Spring Boot 主方法存在。下面的代码为我们生成数据源。
@Autowired
private Environment env;
@Bean
public DataSource dataSource() throws NamingException {
return (DataSource) new JndiTemplate().lookup(env.getProperty("spring.datasource.jndi-name"));
}
还有一件事
如果您想在架构级别访问您的表,请在 @Table(schema = "DB_SCHEMA",name = "DB_TABLE") 中提及架构名称