Datasource Spring boot JNDI数据源查找失败-在上下文中找不到名称comp/env/jdbc;爪哇语:;
我已经设置了一个SpringBoot(v1.1.9)应用程序作为WAR文件部署。我正在尝试将这个web应用程序与现有的数据服务模块(作为maven依赖项添加)集成 尝试部署的环境:WebSphere Application Server 8.5.5.4 我面临的问题是,当尝试在依赖数据服务模块中查找JNDI数据源(jdbc/fileuploads)时,应用程序启动失败Datasource Spring boot JNDI数据源查找失败-在上下文中找不到名称comp/env/jdbc;爪哇语:;,datasource,war,jndi,spring-boot,websphere-8,Datasource,War,Jndi,Spring Boot,Websphere 8,我已经设置了一个SpringBoot(v1.1.9)应用程序作为WAR文件部署。我正在尝试将这个web应用程序与现有的数据服务模块(作为maven依赖项添加)集成 尝试部署的环境:WebSphere Application Server 8.5.5.4 我面临的问题是,当尝试在依赖数据服务模块中查找JNDI数据源(jdbc/fileuploads)时,应用程序启动失败 @Configuration @Profile("prod") public class JndiDataConfig impl
@Configuration
@Profile("prod")
public class JndiDataConfig implements DataConfig {
@Bean
public DataSource dataSource() throws NamingException {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/fileUploadDS");
}
}
我的Spring启动配置:
@Configuration
@ComponentScan(basePackages = { "au.com.aiaa.fileupload.data.*", "demo" })
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class })
public class SampleApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<SampleApplication> applicationClass = SampleApplication.class;
@Bean
public static Properties fileUploadJndiProperties() throws NamingException {
JndiObjectFactoryBean jndiFactoryBean = new JndiObjectFactoryBean();
jndiFactoryBean.setJndiName("props/FileUploadProperties");
jndiFactoryBean.setExpectedType(Properties.class);
jndiFactoryBean.setLookupOnStartup(true);
jndiFactoryBean.afterPropertiesSet();
return (Properties) jndiFactoryBean.getObject();
}
}
我提到了,它说我们需要在servlet容器上设置enableNaming()?我可以为非嵌入式web应用程序上下文执行类似的操作吗?或者这纯粹是WAS8.5版本的问题???您需要在
web.xml
中使用jdbc/fileuploads
名称进行资源引用。并确保在安装期间或通过ibmwebbnd.xml
文件将其绑定到实际的数据源名称
web.xml
中的定义:
<resource-ref>
<description />
<res-ref-name>jdbc/fileUploadDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
但是我不是Spring boot的专家,所以不知道它是否会工作,或者在那里是否可行。我也面临着同样的问题。我不知道如何在SpringBoot中定义标记,因为SpringBoot中没有web.xml文件 到目前为止,我知道我们必须在启动spring应用程序的应用程序文件中定义它。我认为我们需要使用此方法来设置数据源:
@Bean(destroyMethod="")
@ConfigurationProperties(prefix="datasource.mine")
public DataSource dataSource() throws Exception {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
return dataSourceLookup.getDataSource("java:jdbc/configurationFile");
}
我能够将我的Spring引导应用程序(部署在WebSphereApplicationServer9中)连接到WAS数据源。 以下代码适用于我,用于连接到数据源:
@Bean(name = "WASDataSource")
public DataSource WASDataSource() throws Exception {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
return dataSourceLookup.getDataSource("DSNAME");
}
@Bean(name = "WASDataSourceJdbcTemplate")
public JdbcTemplate jdbcTemplate_WASDS(@Qualifier("WASDataSource")
DataSource dsDB2) {
return new JdbcTemplate(dsDB2);
}
注意:数据源“DSNAME”的名称是出现在Websphere控制台UI上的名称。
您可以通过->选择资源>JDBC>数据源看到这一点
然后我创建了jdbc模板:
@Autowired
@Qualifier("WASDataSourceJdbcTemplate")
private JdbcTemplate db2WASTemplate;`
并且使用查询方法运行查询工作正常:db2WASTemplate.query()
我没有创建任何Web.xml或ibm-Web-bnd.xml文件我只是用我的自定义数据源配置了spring boot,如下所示:
@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
在application.properties文件中,我像往常一样定义了所有数据源设置
spring.datasource.driver-class-name= ***
spring.datasource.url= ***
spring.datasource.username= ***
spring.datasource.password= ***
spring.datasource.jndi-name=jdbc/myDB
它与@SpringBootApplication和所有其他默认设置都不兼容
谢谢您是否尝试过没有
java:comp/env/
部分的jndObjectFactoryBean
?您需要在web.xml
中有jdbc/fileuploads
名称的资源引用。并确保在安装过程中或通过ibmwebbnd.xml
文件将其绑定到实际的数据源名称。谢谢@Gas,它可以工作。我在web.xml中添加了资源引用,并在WAS管理控制台中安装时将资源引用绑定到实际的数据源。但是,如果我的spring boot应用程序中没有web.xml,如何实现同样的效果呢?我阅读了如何在SpringBoot配置类中替换或扩展SpringBootServletilizer。还有什么等效的吗?正如我提到的,添加是唯一适合我的要求的选项,因为我不需要在spring boot应用程序中查找数据源,而是在外部依赖项中查找数据源。所以即使我让@Resource工作,这也不是我想要的。@DinushaKariyawasam Ok。顺便说一句,类级别的@Resource
具有与web.xml
中相同的功能。安装应用程序时,将扫描类以查找注释,并创建合并的web.xml。您可以通过管理控制台看到合并的部署描述符。您是对的!这就是我在Java6/JEE5@Resource(name=“jdbc/fileuploads”,type=javax.sql.DataSource.class)公共类TestServlet扩展HttpServlet{}中的方法。我在中提到了“声明资源引用”部分,我们可以@Resource注释任何接受资源注入的Web组件
@Autowired
@Qualifier("WASDataSourceJdbcTemplate")
private JdbcTemplate db2WASTemplate;`
@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
spring.datasource.driver-class-name= ***
spring.datasource.url= ***
spring.datasource.username= ***
spring.datasource.password= ***
spring.datasource.jndi-name=jdbc/myDB