【问题标题】:(Dependency) Spring MVC templates not found for embedded-tomcat spring-boot jar in parent project (gradle)?(依赖性)在父项目(gradle)中找不到嵌入式tomcat spring-boot jar的Spring MVC模板?
【发布时间】:2018-08-26 18:30:41
【问题描述】:

我有一个用于用户管理的小型 Spring Boot 应用程序。
我想将此 UserManagement 应用程序作为依赖项(gradle)包含在我的其他 spring-boot 应用程序中。

我不想将 UserManagement 应用程序作为独立应用程序运行(它需要与父应用程序共享 bean)。

我正在使用 gradle 和 spring-boot 插件。 我已将 UserManagement jar 作为依赖项包含在 spring-boot ExampleApp 中。

UserManagement 应用在端口 8081 上运行嵌入式 tomcat 服务器。
ExampleApp 在端口 8080 上运行嵌入式 tomcat 服务器。

一切正常,除了我的 UserManagement MVC UI。
当我在端口 8081 上点击用户管理应用程序时,找不到我的 MVC 模板。

我似乎无法从父应用程序 ExampleApp 中找到 UserManagement 应用程序的 MVC 模板。
这甚至可以通过 spring-boot 实现吗?

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    是的。你可以这样做。我不确定您是如何共享 bean 的,但我想您也可以这样做。

    SpringApplicationBuilder
    由于“UserManagement”应用程序是一个依赖项,我假设您正在调用一个方法来从您的父应用程序运行它

    例如,您可能正在做这样的事情:

    public class UserManagementApplication {
        private static UserManagementApplication instance = null;
    
        private final SpringApplicationBuilder springApplicationBuilder;
        private ConfigurableApplicationContext context;
    
        private UserManagementApplication() {
           this.springApplicationBuilder = new SpringApplicationBuilder(UserManagementApplication.class);
        }
    
        public static void start() {
            synchronized (UserManagementApplication.class) {
                if (instance == null) {
                    /*
                     ******** If singleton instance is null ***************************************
                     ******** Create a new UserManagementApplication ******************************
                     */
                    instance = new UserManagementApplication();
                }
    
                if (instance.context == null
                        || !instance.context.isActive()) {
                    /*
                     ******** If the context is null or not active ********************************
                     ******** Create new SpringApplication and run it  ****************************
                     ******** Store the resulting ConfigurableApplicationContext in memory ********
                     */
                    instance.context = instance.springApplicationBuilder.run(new String[]{});
                }
            }
        }
    
        public static void stop() {
            synchronized (UserManagementApplication.class) {
                if (instance == null
                        || instance.context == null
                        || !instance.context.isActive()) return;
                /*
                 ******** If the context is active ********************************************
                 ******** Close the context  **************************************************
                 ******** Set context back to null ********************************************
                 */
    
                instance.context.close();
                instance.context = null;
            }
        }
    
        public static ConfigurableApplicationContext getContext() {
            if (instance == null) return null;
            return instance.context;
        }
    }
    

    UserManagement gradle.build 和位置

    1. 因为您不需要将“UserManagement”应用程序作为 可执行 jar,您可以放弃 spring-boot 插件并制作您的 自己的罐子。
    2. 由于您使用的是 tomcat,您可以放置​​模板和静态 META 中的资源和 WEB-INF 中的模板。
      • 资源 => src/main/resources/META-INF/resources/static/
      • 模板 => src/main/resources/WEB-INF/templates/

    例如,您的 gradle 构建可能如下所示:

    buildscript {
        ext {
            springBootVersion = '2.0.0.M7'
        }
        repositories {
            mavenCentral()
            maven { url "https://repo.spring.io/snapshot" }
            maven { url "https://repo.spring.io/milestone" }
            maven { url 'http://repo.spring.io/plugins-release' }
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
            classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE'
            classpath 'org.springframework:springloaded:1.2.6.RELEASE'
        }
    }
    
    ext {
        springBootVersion = '2.0.0.M7'
    }
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'groovy'
    // apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'propdeps'
    apply plugin: 'propdeps-idea'
    
    group = 'com.example.userManagement'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    
    configurations {
        includeInJar
    }
    
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-groovy-templates')
        compile('org.codehaus.groovy:groovy')
    
        includeInJar("org.webjars:bootstrap:4.0.0")
        includeInJar("org.webjars:jquery:3.3.1")
        configurations.compile.extendsFrom(configurations.includeInJar)
    }
    
    idea {
        module {
            inheritOutputDirs = true
        }
    }
    
    jar {
        from configurations.includeInJar.collect { it.isDirectory() ? it : zipTree(it) }
    }
    
    compileJava.dependsOn(processResources)
    

    MVC 配置

    另外,确保你的 MVC 配置知道资源在哪里。
    例如,如果您正在扩展 WebMvcConfigurerAdapter:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/static/**").addResourceLocations("classpath:META-INF/resources/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:META-INF/resources/webjars/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:META-INF/");
    }
    

    MVC 模板

    如果你在 src/main/resources/META-INF/resources/static/js 中有一个“my-bundle.min.js”文件,你可以像这样从你的 mvc 模板中找到它:

    <script src="/resources/static/js/my-bundle.min.js"></script>
    



    我希望这会有所帮助!

    【讨论】:

    • 非常接近我正在做的事情。只需将 ExampleApp applicationContext 传递给我用来启动 UserManagement 应用程序的类,我就可以在两个应用程序之间共享 bean。我喜欢你的单例类,也许我可以在单例类中添加一个私有的 parentApplicationContext 并在 start() 方法中设置它?我会试试这个。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-03-31
    • 2019-07-26
    • 2017-05-17
    • 2017-09-28
    • 2017-07-05
    • 2016-02-15
    • 2016-02-08
    • 2017-09-05
    相关资源
    最近更新 更多