在我的时区早上好
我已经按照以下两个Stack Overflow问题进行了操作:
和
如何在Spring Boot和Spring Data中使用JNDI数据库连接,使用嵌入式Tomcat?
但是都没有起作用。 我正在使用Spring Boot 2。 我想配置嵌入式Tomcat服务器以与JNDI一起工作。 我已经尝试了两种方法:
代码片段:
@SpringBootApplication
public class MyApplication {
public static void main(String... args) { }
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("jdbc/CCC");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
resource.setProperty("url", "jdbc:oracle:thin:@a77k11111188.tt.ddd.test:3000:BHJR00TT00");
resource.setProperty("username", "user");
resource.setProperty("password", "pass");
context.getNamingResources().addResource(resource);
}
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
tomcat.enableNaming();
TomcatWebServer container = super.getTomcatWebServer(tomcat);
for (Container child : container.getTomcat().getHost().findChildren()) {
if (child instanceof Context) {
ClassLoader contextClassLoader = ((Context) child).getLoader().getClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader);
break;
}
}
return container;
}
};
return tomcat;
}
}
然后使用application.properties
spring.datasource.jndi-name=java:comp/env/jdbc/CCC
错误日志:
Unable to start embedded Tomcat
Error creating bean with name 'servletEndpointRegistrar'
Error creating bean with name 'dataSource'
DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/CCC'
.NamingException: Could not create resource factory instance
ClassNotFoundException: org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory
如果我不使用应用程序属性,而是直接在Spring Boot应用程序中配置数据源bean,就像这样:
@Bean(destroyMethod = "")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/CCC");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
错误日志如下:
UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory'
BeanCreationException: Error creating bean with name 'jpaVendorAdapter'
JndiLookupFailureException: JndiObjectTargetSource failed to obtain new target object
NamingException: Could not create resource factory instance
在我的pom文件中,我有以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我已经没有办法了。 提前感谢。 最好的祝福。
SqlSessionFactory
和SqlSessionFactoryBean
吗? - Ayush Kumar