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