【发布时间】:2016-08-13 08:42:55
【问题描述】:
我创建了这个测试项目,由 2 个项目组成:一个使用 spring-boot,一个使用 spring-mvc。它们中的每一个都可以独立工作。 我想要做的是运行 spring-boot 并能够通过加载它的上下文来访问 spring-mvc 项目的网页。 这个项目很简单,我只是想测试一下如何混音。
问题是,当我运行 spring-boot 应用程序时,无法访问 spring-mvc 的页面,因为它没有在构建中添加 webbapp 文件夹(包含 WEB-INF)。 我可以在 spring-boot 应用程序中从 spring-mvc 自动装配服务。
树如下所示:
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-core-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>
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 都是简单的 bean。我没有在这里粘贴他们的内容,因为问题已经太长了,但如果需要我可以添加它们。
【问题讨论】:
-
我想肯定还需要一些配置。您能否将您的项目发布到 github 或直接邮寄给我?如果我实现了,我将对此进行更多研究并发布任何有用的信息。
标签: java spring spring-mvc gradle spring-boot