2016-02-28 99 views
2

我是Spring Boot的新手。我已經瀏覽了這些文檔和幾個教程。我從他們身上了解到,Spring Boot使開發變得容易。 Spring Boot可以輕鬆創建獨立的,生產級的基於Spring的應用程序,您可以「運行」。 直到教程很好。集成Spring Boot和現有的Spring應用程序

但是,假設你已經有一個應用程序(+用SpringMVC休眠+ MySQL的)我可以利用春季啓動在此應用程序?爲了找出答案,我決定將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> 
+0

我嘗試了很多來解決異常,但無法做到這一點。 –

+0

您使用哪些入門模塊來設置Spring引導應用程序? –

+0

請分享您的pom.xml – Avis

回答

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

從上面的錯誤,應用程序無法找到從罐子依賴定義的類。從春季版4.2 AFAIK添加GenericApplicationListener

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

將您的彈簧版本升級到>4.2並重新檢查。

來到dependency managementSpring-boot可以很容易地去。這裏是spring啓動文檔說的。

  • 提供自以爲是的「啓動器」的POM,以簡化你的Maven 配置
  • 自動配置Spring儘可能

我們沒有從數據庫供應商特定的罐子擔心的依賴性太大分開按要求添加。

API

我得到的印象是,Spring引導無法上述種類的應用程序集成。您需要從頭開始使用應用程序。

沒有,當我們有Maven配置我們,它是所有關於他們的依賴性更改根POM。它關於依賴抽象的一切。從根級構建應用程序無疑很容易,但通過遵循maven原則,我們甚至可以使現有應用程序遵循spring引導。

請解釋我如何可以在已經開發的Spring應用程序春天啓動的優勢是什麼?有關這些依賴

研究,分析其根本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> 

除了上述,

  • 什麼行家
  • 的育兒如何更改版本兒童POM

會幫你定製的應用程序。

+0

發生這種情況時,我試圖將早期的春季應用遷移到春季啓動。升級spring-context依賴解決了這個問題。 –

0

Spring Boot爲您管理依賴關係。測試springboot發行版中的所有依賴關係,並與合理的默認值配合使用。可以定義你自己或覆蓋,但建議你不要。請參閱下面的官方文件:

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

春天啓動的每個版本與 Spring框架的基本版本相關的,所以我們強烈建議你不要在自己指定其 版本。

對於現有的應用程序可能很難拿到的版本匹配,它肯定是可行的一切將取決於應用程序的複雜性,時間對你的手和迴歸測試。從遷移路徑的角度評論您管理的所有依賴關係,並讓springboot將其引入應用程序中,例如spring-boot-starter-web已經拉動了mvc 4.2.4,而你的pom將它定義爲4.0.3.RELEASE。這適用於其他依賴性。 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,我可以完全實現具有幾乎相同的依賴關係的企業應用程序爲DB客戶端。但是我們的應用程序在遷移時相當新穎。

1

只想添加,我完成了我所有的R & D並解決了所有問題。我創建了一個Spring Boot應用程序並將其發佈到github上。 https://github.com/gauravdubey58/SpringBootLegacy

在這個應用程序中,我使用Spring MVC和Hibernate的xml配置。

相關問題