gpt4 book ai didi

spring-boot - Spring Boot 2 嵌入式 Tomcat Jndi 数据源配置

转载 作者:行者123 更新时间:2023-12-01 18:39:01 25 4
gpt4 key购买 nike

在我的时区早上好

我已经关注了这两个 Stack Overflow 问题:

Spring Boot Using Embedded Tomcat with JNDI

Howto use JNDI database connection with Spring Boot and Spring Data using embedded 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>

我没有解决方案。提前致谢。谨致问候。

最佳答案

我也面临同样的问题,互联网上的大多数示例都使用TomcatEmbeddedServletContainerFactory 但是,在尝试了几件事之后,我终于能够在我的应用程序中获得 jndi 连接。

我仍在找出问题的确切根本原因,但以下是供您引用的代码。

@SpringBootApplication
public class MybatisJNDISampleApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisJNDISampleApplication.class, args);
}

@Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatWebServer(tomcat);
}

@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
//resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
resource.setName("jdbc/myDatasourceName");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
resource.setProperty("url", "db_url");
resource.setProperty("username", "db_username");
resource.setProperty("password", "db_password");
context.getNamingResources().addResource(resource);
}
};
}
}

以下是我的配置类:

@Configuration
@MapperScan("com.sample.mybatis")
public class DataConfig {

public final String MAPPER_LOCATIONS_PATH = "classpath:mybatis-mappers/*.xml";

@Bean(destroyMethod="")
public DataSource dataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/myDatasourceName");
//bean.setResourceRef(true); // this was previously uncommented
bean.setProxyInterface(DataSource.class);
//bean.setLookupOnStartup(false); // this was previously uncommented
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}

@Bean
public DataSourceTransactionManager transactionManager() throws NamingException {
return new DataSourceTransactionManager(dataSource());
}

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
configureSqlSessionFactory(sessionFactory, dataSource());
return sessionFactory.getObject();
}

public void configureSqlSessionFactory(SqlSessionFactoryBean sessionFactoryBean, DataSource dataSource) throws IOException {
PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(pathResolver.getResources(MAPPER_LOCATIONS_PATH));
}
}

希望这可以帮助您解决问题。

关于spring-boot - Spring Boot 2 嵌入式 Tomcat Jndi 数据源配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52552866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com