问题
I'm currently trying Spring Data Flow Server with Spring Batches. I have two datasources in my application.properties. And the Spring Batch application works fine and it reads and writes data into the Database.
But When I tried to deploy the Jar file in Spring Cloud Data Flow server, the SCDF doesn't load the properties from application.properties file and loads the default h2 config. I also tried passing the config as arguments when the starting the SCDF, but I get Oracle Driver not found in class path. But remember batch jobs work and inserts data. My question here is where I should keep the Oracle Jdbc Driver jar (ojdbc7-1.0.0 ) when passing those database config as arguments during SCDF startup? Or how to add the dependency so the SCDF would see the database config. Below is how I passed Database arguments.
java -jar spring-cloud-dataflow-server-2.4.2.BUILD-20200310.115040-7.jar
--spring.datasource.url=jdbc:oracle:thin:@mydb
--spring.datasource.username=username
--spring.datasource.password=password
--spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
And the exception I got,
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.OracleDriver
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:617)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:605)
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1242)
at org.springframework.boot.actuate.autoconfigure.health.HealthEndpointConfiguration.healthContributorRegistry(HealthEndpointConfiguration.java:78)
I found in another SO question that oracle driver dependency could be added to SCDF. But he didn't mention clearly how, and I do not have enough points to add a comment. Hence posting here. And the link to the answer I'm referring to is,
spring-data-flow task example
I have fixed the same issue by getting SCDF source code & added oracle dependency jars and ran the command same as above. It worked. You need to make sure that you added drivers for DB in SCDF before running the above command. SCDF comes with DB related jars but if you are using oracle or some third party you need to add it manually.
Any help is appreciated.
Note: In my application I have extended the DefaultTaskConfigurer and returned the Oracle Datasource which has Task_Execution and related tables. But this seems to do nothing.
回答1:
According to Spring Cloud Data Flow reference (Section 24.1.1), you should add the dependency for the database driver required (Oracle in your case) in the dependencies
section of your application's pom file.
<dependencies>
...
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
...
</dependencies>
Then you must build the application as described here: Building Spring Cloud Data Flow.
Lucky for us nowdays Oracle's JDBC drivers are available on Maven Central
Check also:
Add Custom JDBC Driver to Spring Cloud Data Flow Server
SCDF Provide better docs on how to add Oracle JDBC Driver
来源:https://stackoverflow.com/questions/61207719/how-to-add-oracle-driver-dependency-to-spring-data-flow-server