2016-04-20 123 views
6

我有我創建的這個測試項目,它由兩個項目組成:一個使用spring-boot,一個使用spring-mvc。他們每個人都獨立工作。 我想要做的是運行spring-boot,並能夠通過加載其上下文來訪問spring-mvc項目的網頁。 該項目非常簡單,因爲我只是想測試如何做混合。在spring-boot項目中使用spring mvc xml項目

問題是,當我運行spring-boot應用程序時,spring-mvc的頁面無法訪問,因爲它不會在構建中添加webbapp文件夾(包含WEB-INF)。 我可以在spring-boot應用程序中自動從spring-mvc中裝入服務。

樹如下所示:

enter image description here

春天啓動的Application.java類是:

package hello; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.ComponentScan; 

import java.util.Arrays; 

@SpringBootApplication 
@ComponentScan({"org.burdu", "hello"}) 
//@ImportResource({"classpath:WEB-INF/spring-core-config.xml", "classpath:WEB-INF/spring-mvc-config.xml"}) 
public class Application { 

public static void main(String[] args) { 
    ApplicationContext ctx = SpringApplication.run(Application.class, args); 

    System.out.println("Let's inspect the beans provided by Spring Boot:"); 

    String[] beanNames = ctx.getBeanDefinitionNames(); 
    Arrays.sort(beanNames); 
    for (String beanName : beanNames) { 
    System.out.println(beanName); 
    } 
} 
} 

根的build.gradle

group 'net.burdu' 
version '1.0-SNAPSHOT' 

apply plugin: 'java' 
apply plugin: 'idea' 

sourceCompatibility = 1.8 

repositories { 
mavenCentral() 
mavenLocal() 
} 

dependencies { 
testCompile group: 'junit', name: 'junit', version: '4.11' 
} 

root settings.gradle

rootProject.name = 'testSpringXMLAndBoot' 
include 'spring-mvc' 
include 'spring-boot' 

彈簧引導的build.gradle

apply plugin: 'java' 
apply plugin: 'idea' 
apply plugin: 'war' 
apply plugin: 'spring-boot' 

sourceCompatibility = 1.8 

repositories { 
jcenter() 
mavenCentral() 
} 

buildscript { 
repositories { 
    mavenCentral() 
} 

dependencies { 
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE") 
} 
} 

dependencies { 
compile("org.springframework.boot:spring-boot-starter-web") 
compile project(':spring-mvc') 
} 

彈簧-MVC的build.gradle

apply plugin: 'java' 
apply plugin: 'idea' 
apply plugin: 'war' 
apply plugin: 'jetty' 

sourceCompatibility = 1.8 

repositories { 
mavenCentral() 
mavenLocal() 
} 

dependencies { 
compile 'ch.qos.logback:logback-classic:1.1.3' 
compile 'org.springframework:spring-webmvc:4.1.6.RELEASE' 
compile 'javax.servlet:jstl:1.2' 
} 

jettyRun{ 
contextPath = "" 
httpPort = 8080 
} 

jettyRunWar{ 
contextPath = "" 
httpPort = 8080 
} 

彈簧核-config.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd "> 

<context:component-scan base-package="org.burdu.web" /> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/view/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<mvc:resources mapping="/resources/**" location="/resources/" /> 

<mvc:annotation-driven /> 

</beans> 

彈簧mvc- config.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd "> 

<context:component-scan base-package="org.burdu.service" /> 

</beans> 

的web.xml內彈簧MVC項目

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5"> 

<display-name>Gradle + Spring MVC Hello World + XML</display-name> 
<description>Spring MVC web application</description> 

<!-- For web context --> 
<servlet> 
    <servlet-name>hello-dispatcher</servlet-name> 
    <servlet-class> 
    org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-mvc-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>hello-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<!-- For root context --> 
<listener> 
    <listener-class> 
    org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-core-config.xml</param-value> 
</context-param> 

</web-app> 

的HelloController中彈簧罩之內,HelloWorldService的和WelcomeController簡單豆。我不會在這裏粘貼他們的內容,因爲這個問題已經太長了,但如果需要的話,我可以添加它們。

+0

我想必須有一些更多的配置需要。你能否把你的項目發佈到github或者只是郵寄給我?如果我已經取得成功,我會做更多的研究併發布任何有用的信息。 –

+0

https://github.com/Burdu/testSpringXMLAndBoot –

回答

3

我已經將組合項目導入到我的IDE中,並且我發現很難將兩個項目合併爲一個並保留原始項目。真正的難題是春天無法知道位於lib/spring-mvc.jar的子項目spring-mvc罐子裏面的資源。如果我將它提取到子項目spring-boot中,它仍然不起作用。

之後,我發現這個文件在春季啓動,它說,大約JSP limitations:因爲在Tomcat中

一個硬編碼的文件模式更重要的

一個可執行的JAR將無法正常工作,我的建議是將您的應用程序轉換爲現代彈簧啓動形式,如下所示:

Convert an existing application to Spring Boot

在這種情況下,您只需將您在web.xml中定義的servlet和過濾器轉換爲spring beans,如文檔所述。此外,spring-core-config.xmlspring-mvc-config.xml可以保持不變,因爲即使它們位於嵌入式jar中,也可以直接導入它們。它應該很容易。

將應用程序轉換爲spring啓動之後,您也可以對Web容器進行全部控制。

最後,如果你真的想一起使用它們,下面的文件可能是有用的:

Deploying a WAR in an Old (Servlet 2.5) Container

這個時候,你可以在你的web.xml指定SpringBootContextLoaderListener爲您的聽衆。它可以幫助您配置您的應用程序。但是,正如春天所說,它只是用於將應用程序部署到舊容器中。

但是,如果我是你,我會將它轉換爲彈簧引導格式。因爲它可以簡化維護工作。

默認情況下,春季開機只允許目錄/META-INF/resourcesresourcespublicstatic作爲資源加載路徑。從這個預期來看,如果你所有的資源都位於這些目錄下,它應該可以工作。當然,需要specifying document root。如果你可以容忍提取spring-mvc,我認爲這將是解決方案。

如果在嵌入式jar中定義的spring上下文是你想要的,你可以在classpath之後加上一個星號,例如classpath*:WEB-INF/applicationContext.xml。這將在任何lib jar中添加任何WEB-INF/applicationContext.xml。換句話說,你只需要在你的@ImportResource表達式上添加一個星號classpath。如果一切順利,你就在那裏。

+0

謝謝。你可以刪除jsp部分。想象一下spring-mvc應用程序只有REST,並且在webapp中你有javascript前端。那可行嗎? –

+0

我試圖將'spring-mvc'解壓縮到'spring-boot'中,它使它成爲'spring-boot'的一部分(如果將'spring-mvc'保存爲嵌入的jar文件,'File'無法識別'spring-mvc'中的資源)。之後,我調試了'spring-boot',我發現它只允許使用目錄'/','META-INF/resources','resources','public','static'作爲資源加載路徑。 –

+0

我已經改進了我的答案,請看看! –

2

我想這些小的變化,以您的github project

春天開機/的build.gradle:

// This will include files and settings from spring-mvc 
war { 
    baseName = 'testSpringXMLAndBoot' 
    version = '0.1.0' 
    from '../spring-mvc/src/main/webapp' 
} 

// tomcat-embed-jasper is for jsp support 
dependencies { 
    .. 
    compile("org.apache.tomcat.embed:tomcat-embed-jasper") 
    .. 
} 

春季啓動/ src目錄/主/ JAVA /你好/應用。Java的:

@SpringBootApplication 
@ComponentScan({"org.burdu", "hello"}) 
@ImportResource({"classpath:WEB-INF/spring-mvc-config.xml"}) // added this resource back 
public class Application { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(Application.class, args); 
     .. 
    } 
} 

現在運行:

  • 彈簧MVC/gradle這個jettyRun

還會Jetty服務器

和運行上獨立運行spring-mvc模塊:

  • 春季啓動/ Java的罐子建立/庫/ testSpringXMLAndBoot-0.1.0.war

將包括spring-mvc上下文spring-boot和嵌入式Tomcat的運行它們。

+0

但是仍然無法使用Spring啓動中的Application.main運行它 –

+0

您能否澄清這意味着什麼?你如何運行它? – janih

+0

我的意思是你可以通過簡單地運行Application.main類來從IDE運行Spring應用程序。在你提供的替代方案中,你無法做到這一點。 –