Spring Executable jar不保存在DB中,它适用于Eclipse

时间:2016-11-17 05:46:11

标签: java spring spring-boot spring-data-jpa executable-jar

我有一个SpringBoot应用程序,当我使用Eclipse运行它时可以正常工作。我创建了一个可运行的jar并测试它。所有工作都需要一件事:它不会在数据库中保存任何内容,也没有要检查的日志或异常。我假设出口必须有东西,因为它不存储任何东西, 甚至没有在本地运行它。

我有两个项目,一个取决于另一个。 项目POM.xml:

<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>
<groupId>BatchCare</groupId>
<artifactId>BatchCare</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.4.2.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>org.simpleframework</groupId>
        <artifactId>simple-xml</artifactId>
        <version>2.6.9</version>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.1.0.7.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
        <!-- <version>1.3.2</version> -->
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>

    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>CareLibrary</groupId>
        <artifactId>CareLibrary</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.156</version>
    </dependency>

</dependencies>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
    <repository>
        <id>local-repo</id>
        <name>Local Project Repo</name>
        <url>file://${basedir}/lib/</url>
        <layout>default</layout>
    </repository>
</repositories>

课程:

@ComponentScan({"ar.com.tr.latam.care.utils",     "ar.com.tr.latam.care.unification.process",    "ar.com.tr.latam.care.repository"})
@EnableJpaRepositories

//@Component
 @Repository
  public class RequestLoggerImpl implements RequestLogger {

/**
 * Log.
 */
private static final Logger LOG = LoggerFactory.getLogger(RequestLoggerImpl.class); 

@Autowired
private RequestLogDao requestLogDao;

@Override
public void log() {

    RequestLog requestLog = new RequestLog();
    requestLog.setDate(new Date());     
    requestLog.setSource("jar");

    try {           
        requestLogDao.save(requestLog);

    } catch (Exception ex) {
        System.out.println("-------------ERROR-------------------");
        ex.printStackTrace();

    }
}
}

RequestLogDAO

public interface RequestLogDao extends CrudRepository<RequestLog, Integer> {

}

RquestLog

@Entity
@Table(name = "REQUEST_LOG")
public class RequestLog {


@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "RequestLogSeq")
@SequenceGenerator(name = "RequestLogSeq", sequenceName = "REQUEST_LOG_SEQ")
private Integer id;

@Column
@NotNull
private Date date;


@NotNull
private String source;

    public void setDate(Date date) {
    this.date = date;
}

public void setSource(String source) {
    this.source = source;
}
}   


}

我的主要班级:

@ComponentScan({"ar.com.tr.latam.care", "ar.com.tr.latam.care.repository", "ar.com.tr.latam.care.filtro"})
 @EnableJpaRepositories
 @SpringBootApplication
 public class InitBatch implements CommandLineRunner {

@Autowired
private Batch batch;

@Override
public void run(String... args) throws Exception {
    batch.processFiles();   
}

public static void main(String[] args) throws Exception {
    long startTime = System.nanoTime();
    SpringApplication.run(InitBatch.class, args).close();       
    System.out.println(String.format("Total process time (%s) seconds",(System.nanoTime() - startTime) / 1000000000));
}

 }

如何导出项目?首先,我为Library项目进行了mvn clean安装。在此之后,我去我的项目并做mvn清洁包。

这会创建一个可运行的Runnable jar,期望这个无法在数据库上保存任何问题的问题。

此外,RequestLogDao是用JPA制作的。

有任何帮助吗?提前谢谢!

0 个答案:

没有答案