Ich habe dieses Testprojekt ich erstellt, aus 2 Projekten komponiert: eines mit Spring-Boot und eines mit Feder-Mvc. Jeder von ihnen funktioniert eigenständig. Ich möchte den Spring-Boot starten und auf die Webseiten des Spring-Mvc-Projekts zugreifen, indem ich seinen Kontext lade. Das Projekt ist ziemlich einfach, da ich nur testen möchte, wie man den Mix macht.Verwenden Sie Federmvc Xml-Projekt im Frühjahr-Boot-Projekt
Das Problem ist, dass, wenn ich die Spring-Boot-Anwendung ausführen, die Seite von Spring-Mvc nicht zugänglich ist, da es nicht den Webbapp-Ordner (mit WEB-INF) im Build hinzufügen. Ich kann den Service von spring-mvc innerhalb der Spring-Boot-Anwendung autowire.
Der Baum sieht wie folgt aus:
Die Application.java Klasse für das Frühjahr-boot ist die folgende:
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);
}
}
}
Die Wurzel 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'
Feder-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')
}
feder 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
}
Federkern-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>
feder 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 innen feder mvc Projekt
<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>
The Hello innen feder Boot, und Helloworld WelcomeController sind einfache Bohnen. Ich gebe hier ihren Inhalt nicht an, da die Frage schon zu lange war, aber wenn nötig, kann ich sie hinzufügen.
Ich denke, es müssen einige weitere Konfigurationen benötigt werden. Könntest du bitte deine Projekte auf GitHub veröffentlichen oder einfach mailen? Ich werde mehr darüber recherchieren und nützliche Informationen veröffentlichen, wenn ich es geschafft habe. –
https://github.com/Burdu/testSpringXMLAndBoot –