Auto-wire Error in Executable JAR Created by STS

Asked 2 years ago, Updated 2 years ago, 125 views

Nice to meet you.
I have experience with .Net, but I don't have much experience with Java, so I may be skipping the common sense of the Java world.Please feel free to point it out.

When I created a batch on STS in a Windows environment and ran on a Linux server (CentOS 7) on a JAR created by Maven Install as a executable JAR, I encountered a runtime error (Log4j error log excerpt below).
*BBBB01 is the program name.
I had a lot of trouble with overseas sites such as StackOverflow in the message, but it was not a strong information.
I tried various settings, but it didn't improve.

What is the cause of this error and what is the solution?
The target version is 1.8

.
YYYY-MM-DD 13:49:33,413 ERROR CommandLineJobRunner - Job Terminated in error: Error creating bean with name 'BBBB01Configuration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.repository.JobRepository com.batch.XXXXXXX.BBBB01Configuration.jobRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class'${batch.jdbc.driver}'
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BBBB01Configuration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.repository.JobRepository com.batch.XXXXXX.BBBB01Configuration.jobRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class'${batch.jdbc.driver}'
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues (AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)

====
The error appears to be occurring before and after autowire the property file, so this information is likely to be relevant.

● There are two property files, both in src/main/resources.
(1)batch.properties
(2)log4j.properties

(1)(2) has been placed individually in the directory where jar is placed in the running environment.

In addition to the general HSQLDB configuration ↓, we have added some program-specific settings to (1).

#Placeholders batch.*
#    for HSQLDB:
batch.jdbc.driver = org.hsqldb.jdbcDriver
batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true
# use this one for a separate server process so you can inspect the results
# (or add it to system properties with -D to override at run time).
# batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples
batch.jdbc.user=sa
batch.jdbc.password=
batch.schema=
batch.schema.script=classpath:/org/springframework/batch/core/schema-hsqldb.sql

● launch-context.xml is defined below under beans.

<context:property-placeholder order="1" location="classpath:batch.properties"/>
<context:property-placeholder order="2" location="classpath:log4j.properties"/>
<context:announcement-config />

<context:component-scan base-package="com.batch.XXXXX"/>
<jdbc: initialize-database data-source="dataSource">
    <jdbc:script location="${batch.schema.script}"/>
</jdbc: initialize-database>

<batch:job-repository id="jobRepository"/>

<import resource="classpath:/META-INF/spring/module-context.xml"/>

● Manifest file generated by Maven (maven-shade-plugin).

Manifest-Version: 1.0
Package: com.batch.XXXXXX
Build-Number:1
Archiver-Version:Plexus Archiver
Built-By: xxxxxxxxxx
Class-Path:./
Created-By—Apache Maven 3.3.9
Build-Jdk—1.8.0_102
Main-Class: org.springframework.batch.core.launch.support.CommandLineJ
 obRunner

なぜかThere was a new line, but it didn't seem to be the cause.

● Excerpts from POM
■ properties

<properties>
    <maven.test.failure.ignore>true</maven.test.failure.ignore>
    <spring.framework.version>3.0.6.RELEASE</spring.framework.version>
    <spring.batch.version>2.1.7.RELEASE</spring.batch.version>
    <configFileBatch>batch.properties</configFileBatch>
    <configFilelog4j>log4j.properties</configFilelog4j>
</properties>

■build

<build>      
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId> maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin> 


    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId> maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <index>false</index>
                <manifest>
                    <packageName>com.batch.xxxxxx;/packageName>
                    <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId> maven-shade-plugin</artifactId>
        <configuration>
            <finalName>BBBB01</finalName>
            <transforms>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <manifestEntries>
                        <Main-Class>org.springframework.batch.core.launch.support.CommandLineJobRunner</Main-Class>
                        <Class-Path>./</Class-Path>
                        <Build-Number>1</Build-Number>
                    </manifestEntries>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                    <resource>.properties</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.schemas</resource>
                </transformer>
            </transforms>
            <archive>
              <manifestEntries>
                <Class-Path>./</Class-Path>
              </manifestEntries>
            </archive>
          </configuration>
        <version>1.7</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

</plugins>

That's it

spring java8

2022-09-30 21:21

1 Answers

Cannot load JDBC driver class'${batch.jdbc.driver}'
is printed.
batch.jdbc.driver seems to be defined in the property file, but what is the setting for the reader?
The XML file describing ${batch.jdbc.driver} might not be able to read the property file?


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.