java - 在 spring-boot 项目中使用 spring mvc xml 项目

标签 java spring spring-mvc gradle spring-boot

我创建了这个测试项目,它由 2 个项目组成:一个使用 spring-boot,一个使用 spring-mvc。他们每个人都可以独立工作。 我想要做的是运行 spring-boot 并能够通过加载其上下文来访问 spring-mvc 项目的网页。 该项目非常简单,因为我只是想测试如何进行混音。

问题是当我运行 spring-boot 应用程序时,无法访问来自 spring-mvc 的页面,因为它没有在构建中添加 webbapp 文件夹(包含 WEB-INF)。 我能够在 spring-boot 应用程序中从 spring-mvc Autowiring 服务。

树看起来如下:

enter image description here

spring-boot 的 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'

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')
}

spring-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
}

Spring 核心配置.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>

spring-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>

spring-mvc项目中的web.xml

<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>

spring-boot 中的 HelloController、HelloWorldService 和 WelcomeController 都是简单的 beans。我不会在这里粘贴他们的内容,因为问题已经太长了,但如果需要,我可以添加它们。

最佳答案

我已将合并的项目导入到我的 IDE 中,但我发现很难将两个项目集成为一个项目并保持它们的原始状态。真正的困难是spring无法知道位于lib/spring-mvc.jar中的子项目spring-mvc的jar里面的资源。如果我将它提取到子项目 spring-boot 中,它仍然不起作用。

后来,我找到了关于 JSP limitations 的文档在 spring-boot 中,它说:

An executable jar will not work because of a hard coded file pattern in Tomcat

此外,我的建议是将您的应用程序转换为现代 spring-boot 形式,如下所示:

Convert an existing application to Spring Boot

在这种情况下,您只需将 web.xml 中定义的 servlet 和过滤器转换为 spring bean,如文档所述。此外,spring-core-config.xmlspring-mvc-config.xml 可以保持不变,因为即使它们在嵌入式jar 中也可以直接导入。这应该很容易。

将应用程序转换为 spring boot 后,您也可以完全控制 Web 容器。

最后,如果你真的想一起使用它们,下面的文档可能会有用:

Deploying a WAR in an Old (Servlet 2.5) Container

这一次,您可以在 web.xml 中将 SpringBootContextLoaderListener 指定为您的监听器。它可以帮助配置您的应用程序。然而,正如 spring 所说,它仅对将您的应用程序部署到旧容器中有用。

但是,如果我是你,我会将其转换为 spring-boot 格式。因为它可以简化维护工作。

默认情况下,spring boot 只允许目录 /,META-INF/resources,resources,public ,static 作为资源加载路径。从这个角度来看,如果您的所有资源都位于这些目录下,它应该可以工作。当然,specifying document root将需要。如果您可以容忍提取 spring-mvc,我认为这就是解决方案。

如果 spring context defined in embedded jar 是你想要的,你可以在 classpath 之后添加一个星号,例如 classpath*:WEB-INF/applicationContext .xml。这将在任何 lib jar 中添加任何 WEB-INF/applicationContext.xml。换句话说,您只需要在 @ImportResource 表达式的 classpath 之后添加一个星号。如果一切顺利,你就在那里。

关于java - 在 spring-boot 项目中使用 spring mvc xml 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36747719/

相关文章:

java - 如何创建 .jar 文件?

java - 覆盖 sbt 0.11.2 中的包名称

java - 在 Oracle 中创建异步填充索引

java - 如何在 Spring JPA/Hibernate 中使用 JoinTable 设置仅通过 ID 引用父实体的子实体

java - Spring MVC 表单处理

spring - 如何更改/更新/删除 Spring ConfigurableEnvironment 中的属性

java - 使用 Java 从元素中删除所有属性

java - 将默认公共(public)构造函数添加到生成的构建器内部类

java - 以编程方式调用@Controller

spring - JUnit测试用例错误: "Address already in use: bind"