繁体   English   中英

在 spring mvc 应用程序中运行 spring 启动应用程序

[英]run spring boot application in spring mvc application

I try to run spring boot app inside spring mvc by adding spring boot app dependency in spring mvc pom and scan spring boot package but i faced below issue

    ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name anonymousFavouriteController: Unsatisfied dependency expressed through field batchFileUploadService; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name batchFileUploadServiceImpl: Unsatisfied dependency expressed through field uploadedFilesRepo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [.bup.repository.UploadedFilesRepo]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

您在单个应用程序中有多个 Spring 上下文。 它通常是一个合法的配置,但在复杂的应用程序(并且 MVC 应用程序总是复杂的)的情况下,让两者都运行肯定不容易。

我至少看到两个问题:

组件扫描两个应用程序都会扫描包并查找属于该应用程序的类。 您必须检查包列表是否重叠。

DispatcherServlet每个应用程序都会尝试注册它自己的 DispatcherServlet。 您需要 map 每个 servlet 只针对不重叠的选定路径。


Spring MVC 和 Spring Boot 可以通过多种不同的方式进行配置。 它可以通过web.xml ,注释,Spring Z3501BB093D363810B8710这些方法的最狂野组合9个描述符和5个组合来完成。 我将开始使用web.xml进行配置。 像这样的东西:

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

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

<servlet-mapping>
    <servlet-name>app1</servlet-name>
    <url-pattern>/app1/*</url-pattern>
</servlet-mapping>

<!-- All other urls go to "app2" -->
<servlet-mapping>
    <servlet-name>app2</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

XML 描述符都将定义要扫描两个应用程序的包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM