简体   繁体   English

在spring-boot-maven-plugin中设置mainClass导致我需要显式设置'defaultServletName'属性

[英]Setting mainClass in spring-boot-maven-plugin causes me to need to set the 'defaultServletName' property explicitly

I had a springboot jar working well, until I added newrelic-agent jar to the classpath, now I need to specify a main-class. 我有一个springboot jar运行良好,直到将newrelic-agent jar添加到类路径中,现在我需要指定一个主类。 This makes sense because I now have several classes with a static main args method in the classpath. 这是有道理的,因为我现在在类路径中有几个带有静态main args方法的类。 The recommended way to do this is to add it to the'spring-boot-maven-plugin'. 推荐的方法是将其添加到“ spring-boot-maven-plugin”中。 So I did the following: 所以我做了以下事情:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <mainClass>com.ebates.rclon.Application</mainClass>
  </configuration>
  <executions>
    <execution>
     <goals>
        <goal>repackage</goal>
     </goals>
    </execution>
  </executions>
</plugin>

After doing that I get: 完成后,我得到:

java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. java.lang.IllegalStateException:无法找到用于提供静态内容的默认servlet。 Please set the 'defaultServletName' property explicitly. 请明确设置“ defaultServletName”属性。

  • Why would specifying a main-class break spring boots autoconfiguration? 为什么要指定主类中断弹簧靴子自动配置?
  • How was the defaultServletName configured earlier? 先前如何配置defaultServletName?

I think I have a better understanding of this after some research and playing around. 经过一些研究和研究,我认为我对此有了更好的理解。

  • It wasn't that adding a mainClass was the issue. 不是要添加mainClass才是问题。 The Spring autoconfiguration had nothing to do with it, the issue is that since there are two jars in the classpath, a second main method was introduced and spring boot doesn't know which one to use when launching. Spring自动配置与此无关,问题在于,由于类路径中有两个jar,因此引入了第二种主要方法,并且Spring Boot在启动时不知道使用哪个。 By fixing that issue also revealed that there are multiple application contexts. 通过修复该问题还显示出存在多个应用程序上下文。

  • In Spring MVC there is a default servlet context that is used when no other context if found for serving static content. 在Spring MVC中,有一个默认的servlet上下文,如果找不到其他上下文来提供静态内容,则使用该默认servlet上下文。 When you enable this usually you do not specify a name, but if there are multiple candidate contexts in your classpath you need to name it. 启用此选项后,通常不指定名称,但是如果您的类路径中有多个候选上下文,则需要命名。 Here is an example on how to do that: 这是有关如何执行此操作的示例:

@Configuration public class MVCConfig extends WebMvcConfigurerAdapter
{

    @Override public void configureDefaultServletHandling(final 
                       DefaultServletHandlerConfigurer configurer)
    {
        configurer.enable("nameOfDefaultContextGoesHere");
    }

}

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

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