简体   繁体   English

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

[英]Integrate Spring Boot with existing Spring application

I am new to Spring Boot. 我是Spring Boot的新手。 I have gone through the documents and few tutorials. 我浏览了文档和一些教程。 What I understood from them, Spring Boot makes development easy. 从他们那里我了解到,Spring Boot使开发变得容易。 Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". 使用Spring Boot可以轻松地创建独立的,基于生产级别的基于Spring的应用程序,您可以“运行”它们。 Till tutorials it was fine. 直到教程还好。

But suppose you already have an application (SpringMVC+Hibernate+MySql) can I take advantage of Spring Boot in this application? 但是,假设您已经有一个应用程序(SpringMVC + Hibernate + MySql),可以在该应用程序中利用Spring Boot吗? To figure out answer I decided to integrate Spring Boot in one project. 为了弄清楚答案,我决定将Spring Boot集成到一个项目中。 I downloaded a sample working code from http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial and tried to integrate Spring Boot. 我从http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial下载了示例工作代码,并尝试集成Spring Boot。 I am not able to get this working. 我无法使它正常工作。 I am getting below exception. 我在例外之下。

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. 我得到的印象是Spring Boot无法集成在上述类型的应用程序上。 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应用程序中利用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>
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. 由于上述错误,应用程序无法从jar依赖项中找到定义的类。 GenericApplicationListener is added from spring version 4.2 AFAIK. GenericApplicationListener是从春季版本4.2 AFAIK添加的。

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

Upgrade your spring version to >4.2 and recheck. 将您的春季版本升级到>4.2然后重新检查。

Here comes the way to dependency management . 这就是dependency management的方式。 Spring-boot makes it easy to go. Spring-boot使它易于使用。 Here is what spring boot documentation says. 这是Spring Boot文档所说的内容。

  • Provide opinionated 'starter' POMs to simplify your Maven configuration 提供有思想的“入门” POM,以简化您的Maven配置
  • Automatically configure Spring whenever possible 尽可能自动配置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 API

I am getting impression that Spring Boot cannot be integrated above kind of application. 我得到的印象是Spring Boot无法集成在上述类型的应用程序上。 You need to devlop application from scratch. 您需要从头开始开发应用程序。

No, When we have the maven configuration with us, it is all about changing the root pom with their dependencies. 不,当我们拥有Maven配置时,一切都与更改根pom及其依赖关系有关。 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的原理,我们甚至可以使现有应用程序遵循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. 研究这些依赖性,分析其根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. 在某种程度上可以帮助我们进行构建配置以及更多样板依赖。

<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 在Maven中育儿是什么
  • How to change version in child pom 如何更改子Pom中的版本

Will help you in customizing the applications. 将帮助您自定义应用程序。

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. 我创建了一个Spring Boot应用程序,并通过github发布了它。 https://github.com/gauravdubey58/SpringBootLegacy https://github.com/gauravdubey58/SpringBootLegacy

In this application I am using Spring MVC and Hibernate with xml configuration. 在此应用程序中,我将使用带有xml配置的Spring MVC和Hibernate。

Spring Boot manages dependencies for you. Spring Boot为您管理依赖关系。 All dependencies in a release for springboot are tested to work together with sensible defaults. 经过测试,springboot发行版中的所有依赖项都可以与合理的默认值一起使用。 It is possible to define you own or override, but its recommended that you do not. 可以定义您自己或覆盖,但建议您不要。 Refer to official documentation below : 请参阅下面的官方文档:

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框架的基本版本相关联,因此我们强烈建议您不要自行指定其版本。

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 eg spring-boot-starter-web is already pulling a spring mvc 4.2.4 while your pom define it as 4.0.3.RELEASE. 从迁移路径的角度注释所有由您管理的依赖项,并让springboot将其拉入应用程序,例如spring-boot-starter-web已经在将pom定义为4.0.3.RELEASE的同时拉入了spring mvc 4.2.4。 This applies to other dependencis. 这适用于其他依赖关系。 Hibernate and spring orm will be pulled by spring-boot-starter-data-jpa. 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. 通过为数据库客户端使用spring-boot-starter-web,spring-boot-starter-data-jpa,spring-boot-starter安全性和spring-boot-starter-freemarker和jar,我具有功能几乎完全相同的企业应用程序。 But our application was fairly new when we migrated. 但是,当我们迁移时,我们的应用程序还很新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM