Sab*_*han 5 tomcat spring-batch spring-data spring-boot
之前已经提出了类似的问题,我经历了所有这些问题但却无法解决问题.相关问题 - Q1,Q2,Q3,Q4,Q5,Q6
我有一个Spring Batch项目与Spring Boot并尝试使用数据库连接池.我使用的是版本为8.5.x的嵌入式tomcat容器.
如果我使用application.properties指定数据源和池设置,一切正常.
但是当我尝试使用JNDI时,我得到了异常 -
Caused by: java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory
Run Code Online (Sandbox Code Playgroud)
我没有tomcat-dbcp-**在Maven jar中看到任何jar名称,因此我不确定是否需要包含任何新的依赖项或需要设置默认数据源工厂以及如何进行操作.
下面是我设置的JNDI bean,问题.我已经消除了某些价值观.
@Bean
public TomcatEmbeddedServletContainerFactory embeddedServletContainerFactory(){
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("jdbc/myDataSource");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "com.ibm.db2.jcc.DB2Driver");
resource.setProperty("url", "url");
resource.setProperty("username", "user");
resource.setProperty("password", "*****");
context.getNamingResources().addResource(resource);
}
};
}
@Lazy
@Bean(destroyMethod="")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/myDataSource");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}
Run Code Online (Sandbox Code Playgroud)
我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<groupId>***</groupId>
<artifactId>***</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>db2</groupId>
<artifactId>db2jcc</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>db2</groupId>
<artifactId>db2jcc_license_cu</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.0.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
Sab*_*han 12
我通过factory在我的Resource定义中设置属性来解决问题.resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
@Bean
public TomcatEmbeddedServletContainerFactory embeddedServletContainerFactory(){
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("jdbc/myDataSource");
resource.setType(DataSource.class.getName());
resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
resource.setProperty("driverClassName", "com.ibm.db2.jcc.DB2Driver");
resource.setProperty("url", "url");
resource.setProperty("username", "user");
resource.setProperty("password", "*****");
context.getNamingResources().addResource(resource);
}
};
}
Run Code Online (Sandbox Code Playgroud)
根据tomcat 8文档,它应该通过查看DataSource类型自动推断数据库池工厂类型,并以某种方式默认为DBCP工厂,并且该类在我的类路径中不存在.
我想这样的问题可以通过制作tomcat-dbcp-**罐子来解决,但我不知道如何使用弹簧靴启动,或者即使弹簧启动也可以.
我发现奇怪的是Spring Boot不包括tomcat-dbcp依赖项作为启动POM的一部分,但使用DBCP DataSource工厂作为默认工厂.
| 归档时间: |
|
| 查看次数: |
6330 次 |
| 最近记录: |