您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页从Spring异常学习Spring

从Spring异常学习Spring

来源:二三娱乐

我在简书上面搜索了一下spring, 有2655篇文章,我相信这些文章会持续增长下去,因为Spring代码写得太好了,并且我相信这里面不缺乏好的文章,但是我今天想写的我在简书上面搜索了一下,发现没有这一类的文章,那就是如何看spring的异常。

Paste_Image.png Paste_Image.png
  • 1、环境
@Configuration
@EnableSwagger
public class SsdSwaggerConfig {

    private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }
 ................

这是之前在使用Swagger的时候配置的,现在要大范围扩展到其他项目中,需要和spring进行整合。

首先我在Spring配置文件中使用了这个配置

<context:component-scan base-package="net.shopin" />
这个配置说明它会在net.shopin包下去扫描,并且会查找@Component,@Controller,@Service,@Repository等注解标记的组件。

然后我启动了项目,出现以下异常

Paste_Image.png

那么我们就进入正题,从异常开始分析:

我把异常以分号“;”进行拆分

Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssdSwaggerConfig': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void net.shopin.config.SsdSwaggerConfig.setSpringSwaggerConfig(com.mangofactory.swagger.configuration.SpringSwaggerConfig); 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mangofactory.swagger.configuration.SpringSwaggerConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.mangofactory.swagger.configuration.SpringSwaggerConfig.handlerMappings; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

在此之前,我们稍微理解下在这里spring为我们做的事情

  • 为我们创建并且管理Bean

  • 2、 分析异常

咱们先看一下异常:

  • Context initialization failed这是初始化spring上下文失败,因为在使用Spring框架之前Spring要进行上下文的初始化,为我们准备一些环境。
  • BeanCreationException...Injection of autowired dependencies failed 创建Bean异常,因为是注入依赖失败
  • BeanCreationException...Could not autowire method:setSpringSwaggerConfig
    不能注入这个方法:setSpringSwaggerConfig
  • BeanCreationException->Could not autowire field:handlerMappings
Paste_Image.png
  • NoSuchBeanDefinitionException->RequestMappingHandlerMapping:没有找到这个Bean。
@Bean
  public List<RequestMappingHandlerMapping> swaggerRequestMappingHandlerMappings() {
    return handlerMappings;
  }

在类SsdSwaggerConfig中,我们可以看到@Configuration注解,查看@Configuration原码可以看到:它包含了4个注解:

  • @Target

  • @Retention

  • @Documented

  • @Component

  • 3、分析
    我们使用<context:component-scan base-package="net.shopin" />扫描项目,默认包是net.shopin,会扫描到SsdSwaggerConfig这个类,但是在@AutoWired注入setSpringSwaggerConfig的时候,需要SpringSwaggerConfig,但是SpringSwaggerConfig没有被Spring管理到,就会出现之前的那个异常,因为Spring异常会将之后的所有的连带异常全部打印出来,就会导致异常很多,所以我们刚开始只需要看第一条异常信息就可以了,如果想深入分析后面的异常,再看后面的异常信息即可。

  • 4、解决方法
    知道了出现异常的原因,我们如何解决呢?有两种方式:

  • 手动在spring中加入这个Bean
    <bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig"/>

  • 自从注解
    <mvc:annotation-scan/>

<mvc:annotation-scan/>是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务