簡體   English   中英

將Spring Boot與現有的Spring應用程序集成

[英]Integrate Spring Boot with existing Spring application

我是Spring Boot的新手。 我瀏覽了文檔和一些教程。 從他們那里我了解到,Spring Boot使開發變得容易。 使用Spring Boot可以輕松地創建獨立的,基於生產級別的基於Spring的應用程序,您可以“運行”它們。 直到教程還好。

但是,假設您已經有一個應用程序(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

現在我完全困惑了。 我得到的印象是Spring Boot無法集成在上述類型的應用程序上。 您需要從頭開始開發應用程序。 不確定我是否正確。 請解釋一下如何在已經開發的Spring應用程序中利用Spring Boot的優勢?

下面是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

由於上述錯誤,應用程序無法從jar依賴項中找到定義的類。 GenericApplicationListener是從春季版本4.2 AFAIK添加的。

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

將您的春季版本升級到>4.2然后重新檢查。

這就是dependency management的方式。 Spring-boot使它易於使用。 這是Spring Boot文檔所說的內容。

  • 提供有思想的“入門” POM,以簡化您的Maven配置
  • 盡可能自動配置Spring

除了根據要求添加特定於數據庫供應商的jar之外,我們不必擔心依賴項。

API

我得到的印象是Spring Boot無法集成在上述類型的應用程序上。 您需要從頭開始開發應用程序。

不,當我們擁有Maven配置時,一切都與更改根pom及其依賴關系有關。 它全部與依賴抽象有關。 從根級別構建應用程序無疑很容易,但是通過遵循Maven的原理,我們甚至可以使現有應用程序遵循spring-boot。

請解釋一下如何在已經開發的Spring應用程序中利用Spring Boot的優勢?

研究這些依賴性,分析其根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>

除了上述以外,

  • 在Maven中育兒是什么
  • 如何更改子Pom中的版本

將幫助您自定義應用程序。

只是想補充一下,我完成了所有研發工作並解決了所有問題。 我創建了一個Spring Boot應用程序,並通過github發布了它。 https://github.com/gauravdubey58/SpringBootLegacy

在此應用程序中,我將使用帶有xml配置的Spring MVC和Hibernate。

Spring Boot為您管理依賴關系。 經過測試,springboot發行版中的所有依賴項都可以與合理的默認值一起使用。 可以定義您自己或覆蓋,但建議您不要。 請參閱下面的官方文檔:

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

每個Spring Boot版本都與Spring框架的基本版本相關聯,因此我們強烈建議您不要自行指定其版本。

對於現有的應用程序,可能很難獲得版本匹配,這肯定是可行的,這取決於應用程序的復雜性,所需的時間以及回歸測試。 從遷移路徑的角度注釋所有由您管理的依賴項,並讓springboot將其拉入應用程序,例如spring-boot-starter-web已經在將pom定義為4.0.3.RELEASE的同時拉入了spring mvc 4.2.4。 這適用於其他依賴關系。 Hibernate和Spring Orm將由spring-boot-starter-data-jpa拉動。

通過為數據庫客戶端使用spring-boot-starter-web,spring-boot-starter-data-jpa,spring-boot-starter安全性和spring-boot-starter-freemarker和jar,我具有功能幾乎完全相同的企業應用程序。 但是,當我們遷移時,我們的應用程序還很新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM