java 将 Spring Boot 与现有的 Spring 应用程序集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35685409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:27:19  来源:igfitidea点击:

Integrate Spring Boot with existing Spring application

javaspringhibernatespring-mvc

提问by GD_Java

I am new to Spring Boot. I have gone through the documents and few tutorials. What I understood from them, Spring Boot makes development easy. Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". Till tutorials it was fine.

我是 Spring Boot 的新手。我已经浏览了文档和一些教程。我从他们那里了解到,Spring Boot 使开发变得容易。Spring Boot 可以轻松创建独立的、生产级的基于 Spring 的应用程序,您可以“直接运行”这些应用程序。直到教程都很好。

But suppose you already have an application (SpringMVC+Hibernate+MySql) can I take advantage of Spring Boot in this application? To figure out answer I decided to integrate Spring Boot in one project. I downloaded a sample working code from http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorialand tried to integrate Spring Boot. I am not able to get this working. I am getting below exception.

但是假设您已经有一个应用程序(SpringMVC+Hibernate+MySql),我可以在这个应用程序中利用 Spring Boot 吗?为了找出答案,我决定将 Spring Boot 集成到一个项目中。我从http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial下载了一个示例工作代码,并尝试集成 Spring Boot。我无法让这个工作。我低于异常。

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.logging.ClasspathLoggingApplicationListener
at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385)
at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:263)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
at com.journaldev.spring.Application.main(Application.java:14)

Caused by: java.lang.NoClassDefFoundError: 
org/springframework/context/event/GenericApplicationListener

Now I am completely confused. I am getting impression that Spring Boot cannot be integrated above kind of application. You need to devlop application from scratch. Not sure whether I am correct or not. Please explain me how can I take advantages of Spring Boot in already developed Spring application?

现在我完全糊涂了。我的印象是 Spring Boot 无法集成到此类应用程序之上。您需要从头开始开发应用程序。不确定我是否正确。请解释我如何在已经开发的 Spring 应用程序中利用 Spring Boot?

Below is the pom.xml

下面是 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.journaldev.spring</groupId>
<artifactId>SpringMVCHibernate</artifactId>
<name>SpringMVCHibernate</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
    <org.aspectj-version>1.7.4</org.aspectj-version>
    <org.slf4j-version>1.7.5</org.slf4j-version>
    <hibernate.version>4.3.5.Final</hibernate.version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- Apache Commons DBCP -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>
</project>

回答by VinayVeluri

Caused by: java.lang.NoClassDefFoundError: 
org/springframework/context/event/GenericApplicationListener

From the above error, application is unable to find the class defined from the jar dependencies. GenericApplicationListeneris added from spring version 4.2AFAIK.

从上面的错误中,应用程序无法找到从 jar 依赖项中定义的类。GenericApplicationListener从 spring 版本4.2AFAIK添加。

<org.springframework-version>4.0.3.RELEASE</org.springframework-version>

Upgrade your spring version to >4.2and recheck.

将您的 spring 版本升级到>4.2并重新检查。

Here comes the way to dependency management. Spring-bootmakes it easy to go. Here is what spring boot documentation says.

方法来了dependency managementSpring-boot让它很容易去。这是 spring boot 文档所说的。

  • Provide opinionated 'starter' POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • 提供自以为是的“入门”POM 以简化您的 Maven 配置
  • 尽可能自动配置 Spring

We don't have to worry about the dependencies much apart from the Database vendor specific jars to be added as per requirement.

除了要根据要求添加的数据库供应商特定 jar 之外,我们不必担心依赖关系。

API

应用程序接口

I am getting impression that Spring Boot cannot be integrated above kind of application. You need to devlop application from scratch.

我的印象是 Spring Boot 无法集成到此类应用程序之上。您需要从头开始开发应用程序。

No, When we have the maven configuration with us, it is all about changing the root pom with their dependencies. Its all about dependency abstraction. Building application from root level is undoubtedly easy but by following the principles of maven, we can even make the existing application to follow spring-boot.

不,当我们拥有 maven 配置时,这一切都是为了更改根 pom 及其依赖项。它完全是关于依赖抽象。从根级别构建应用程序无疑是容易的,但是通过遵循 maven 的原则,我们甚至可以使现有应用程序遵循 spring-boot。

Please explain me how can I take advantages of Spring Boot in already developed Spring application?

请解释我如何在已经开发的 Spring 应用程序中利用 Spring Boot?

Study about these dependencies, Analyse their root POM. They give us all the dependencies that you add in normal application. In a way helps us with build configurations and many more boiler plate dependencies.

研究这些依赖关系,分析它们的根 POM。它们为我们提供了您在普通应用程序中添加的所有依赖项。在某种程度上帮助我们构建配置和更多样板依赖。

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

Apart from the above,

除了以上,

  • What is parenting in maven
  • How to change version in child pom
  • 什么是 maven 中的育儿
  • 如何在子 pom 中更改版本

Will help you in customizing the applications.

将帮助您定制应用程序。

回答by GD_Java

Just want to add, I finished all my R&D and solve all the problems. I created an Spring Boot application and published it over github. https://github.com/gauravdubey58/SpringBootLegacy

只是想补充一下,我完成了所有的研发并解决了所有的问题。我创建了一个 Spring Boot 应用程序并通过 github 发布它。 https://github.com/gauravdubey58/SpringBootLegacy

In this application I am using Spring MVC and Hibernate with xml configuration.

在这个应用程序中,我使用带有 xml 配置的 Spring MVC 和 Hibernate。

回答by jbhardwaj

Spring Boot manages dependencies for you. All dependencies in a release for springboot are tested to work together with sensible defaults. It is possible to define you own or override, but its recommended that you do not. Refer to official documentation below :

Spring Boot 为您管理依赖项。springboot 版本中的所有依赖项都经过测试,可以与合理的默认值一起工作。可以定义您拥有或覆盖,但建议您不要。参考以下官方文档:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-dependency-management

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-dependency-management

Each release of Spring Boot is associated with a base version of the Spring Framework so we highly recommend you to not specify its version on your own.

Spring Boot 的每个版本都与 Spring Framework 的基本版本相关联,因此我们强烈建议您不要自行指定其版本。

For existing application it may be hard to get versions match, it surely is doable all that will depend on application complexity, time on your hand and regression testing. From migration path standpoint comment all dependencies managed by you and let springboot pull that into application e.g. spring-boot-starter-web is already pulling a spring mvc 4.2.4 while your pom define it as 4.0.3.RELEASE. This applies to other dependencis. Hibernate and spring orm will be pulled by spring-boot-starter-data-jpa.

对于现有的应用程序,可能很难获得版本匹配,这肯定是可行的,这取决于应用程序的复杂性、手头的时间和回归测试。从迁移路径的角度注释所有由您管理的依赖项,并让 springboot 将其拉入应用程序,例如 spring-boot-starter-web 已经拉取了一个 spring mvc 4.2.4,而您的 pom 将其定义为 4.0.3.RELEASE。这适用于其他依赖项。Hibernate 和 spring orm 将被 spring-boot-starter-data-jpa 拉取。

I have fully functional enterprise application with almost same dependencies by using spring-boot-starter-web, spring-boot-starter-data-jpa, spring-boot-starter security and spring-boot-starter-freemarker and jar for DB client. But our application was fairly new when we migrated.

通过使用 spring-boot-starter-web、spring-boot-starter-data-jpa、spring-boot-starter security 和 spring-boot-starter-freemarker 以及用于 DB 客户端的 jar,我拥有功能齐全的企业应用程序,并且依赖项几乎相同。但是当我们迁移时,我们的应用程序是相当新的。