【问题标题】:IllegalArgumentException: A ServletContext is requiredIllegalArgumentException:需要 ServletContext
【发布时间】:2015-09-29 06:06:48
【问题描述】:

无法让我的 Spring Boot 应用程序在 AWS 实例上运行。它在我的机器上运行良好,但看起来自动装配在一个环境中正确解析,但在另一个环境中却没有。看起来我需要稍微清理一下配置类。任何想法在这里?非常感谢。

 **Main class:**

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan
public class Data {



public static void main(String[] args) throws Exception {
    SpringApplication.run(Data.class, args);

}
   }

配置

     @EnableWebMvc
     @Configuration

    public class AquilaDataWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/charts/**").addResourceLocations(
            "file:///var/lib/aquila/");
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }

    @Bean
    public InternalResourceViewResolver defaultViewResolver() {
        // Need this so we can forward to index.html.
        return new InternalResourceViewResolver();
    }
}

例外:

Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)


2015-07-09 19:30:55.773 ERROR 18723 [main] --- o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:601)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1008)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at 

【问题讨论】:

标签: java spring spring-mvc tomcat spring-boot


【解决方案1】:

我假设您的包中有其他 @Configuration,它们由您的 @ComponentScan 选择(出现在您的异常中的 DelegatingWebMvcConfiguration 很可能是由外部@Configuration 中的某处的@EnableWebMvc 导入的)。

可能的解决方案是在组件扫描中使用过滤器。

@ComponentScan( excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })

如果可能,还可以在您的@ComponentScan 中包含basePackages = { "org.yourpackage" }

如果可能,也尝试包含以获得更好的性能

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

它告诉 Spring 将容器的默认 servlet 用于某些请求,例如静态资源。

【讨论】:

  • 不能在主类中使用 excludeFilters,因为这也会排除我想要的所有其他配置。
  • 这里有什么建议吗?这个异常现在正在杀死我。
  • 对我来说同样的问题,我也不能排除配置类。我松了一口气,我不是唯一一个!
【解决方案2】:

你可以使用 Spring Cloud

       <dependencies> 
    <dependency>
 <groupId>org.springframework.cloud</groupId> 

    <artifactId>spring-cloud-aws-autoconfigure</artifactId>
     <version>{spring-cloud-version}</version> 
    </dependency>
    </dependencies>

Spring Cloud AWS 还提供专用的 Spring Boot 支持。 Spring Cloud AWS 可以使用 Spring Boot 属性进行配置,并且还会根据常规设置自动猜测任何合理的配置。

这里还有一个example,它使用 Elastic Beanstalk 在 AWS 上进行部署。

War 变体和 Jar 变体之间有两个不同之处。 War 变体不需要嵌入式 tomcat,因为它将部署在 tomcat 服务器中,因此 pom.xml 将 spring-boot-starter-tomcat 依赖项设置为“provided”。 Jar 变体删除了范围标记,以便在 jar 中包含此依赖项。

org.springframework.boot    spring-boot-starter-tomcat  provided 

第二个区别是ServletInitializer

  public class ServletInitializer extends SpringBootServletInitializer { 
@Override   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootAwsApplication.class);     
} }

现在您已经创建了这个应用程序,我们需要生成用于部署到 Amazon AWS 的 war 文件。右键单击 pom.xml 并选择 Run As->Maven Install。这将运行构建并在应用程序的目标文件夹中创建 war 文件。

使用 Amazon Elastic Beanstalk 部署您的应用程序

1) 登录亚马逊 AWS。 2) 在主控制面板中,选择 Deployment & Management 下的 Elastic Beanstalk。 3) 点击右上角的创建应用程序。 4) 输入应用程序名称,然后单击下一步。 5) 环境层——Web 服务器 6)预定义配置——Tomcat 7) 环境类型——单实例 8) 点击下一步 9) 选择上传您自己的,点击浏览并找到您之前创建的战争。 10) 上传应用程序后,您将看到选择 URL 的下一页。 11) 输入名称并单击检查可用性以查看是否可以使用它。 12) 点击下一步 13) 在这个例子中我们不需要 RDB,所以点击下一步。 14) 在下一步中,您将定义将要创建的 EC2 实例,如果您使用免费试用版,则坚持使用免费 t1.micro 实例类型。 15) EC2 Key Pair,可以不选。你现在不需要它,很可能你还没有配置。这将在稍后的帖子中介绍。 16) 单击下一步。 17)在环境标签中再次单击下一步,因为我们不关心这个。 18) 检查配置,然后单击启动。

【讨论】:

  • 如果 Spring Cloud 方法和 Elastic Beanstalk 方法都不起作用,请告诉我
  • 我不认为现在这与 AWS 有任何关系。我只是想在那里的 linux 服务器上安装这个应用程序。问题出在配置上。我确定。
【解决方案3】:

在您的application.properties(或application.yml)中,确保spring.main.web-environment 未设置为false。这应该可以解决缺少 servlet 上下文的问题。

【讨论】:

  • Bingo。这就是问题所在。 spring.main.web-environment 设置为 false。只需将其更改为 true 即可解决此问题。非常感谢。我现在可以安然入睡了。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 2011-01-10
  • 1970-01-01
  • 2014-02-16
  • 2014-05-22
  • 2014-02-26
  • 1970-01-01
相关资源
最近更新 更多