Spring異常分析

異常報錯java

2019-01-14 10:40:18.427 ERROR 11776 --- [ost-startStop-1] o.s.b.w.e.t.TomcatStarter                : Error starting Tomcat context. 

Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: 


Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 

Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: 


Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException:



 Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: 


 Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration':
  Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: 


  Failed to instantiate [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$caa88ea6]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: 

  Error creating bean with name 'dataSource': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: 

  Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

關鍵在最後一句Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver classmysql

我這裏是有配置mysql驅動的。因此定位應該是配置問題。web

根據提示,能夠看出是從DataSourceProperties這裏拋出來的異常。讀取spring.datasource開頭的配置項。若是spring.datasource.driverClassName爲空,會拋出異常,提示Failed to determine a suitable driver classspring

個人配置文件裏是有配置的,該配置在讀取時會將driver-class-name轉換爲駝峯式寫法driverClassName。因此問題應該是Springboot沒有加載application.properties的問題,能夠來驗證一下。sql

啓動後,異常信息變成app

Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine suitable jdbc url測試

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {


        public String determineDriverClassName() {
        if (StringUtils.hasText(this.driverClassName)) {
            Assert.state(driverClassIsLoadable(),
                    () -> "Cannot load driver class: " + this.driverClassName);
            return this.driverClassName;
        }
        String driverClassName = null;
        if (StringUtils.hasText(this.url)) {
            driverClassName = DatabaseDriver.fromJdbcUrl(this.url).getDriverClassName();
        }
        if (!StringUtils.hasText(driverClassName)) {
            driverClassName = this.embeddedDatabaseConnection.getDriverClassName();
        }
        if (!StringUtils.hasText(driverClassName)) {
            throw new DataSourceBeanCreationException(
                    "Failed to determine a suitable driver class", this,
                    this.embeddedDatabaseConnection);
        }
        return driverClassName;
    }


}

測試加載的配置文件ui

public class KzfApiAlipayApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(KzfApiAlipayApplication.class, args);
        System.out.println(context.getEnvironment().getProperty("spring.datasource.driver-class-name"));
    }

}
public class ServletEndpointManagementContextConfiguration {

        @Configuration
    @ConditionalOnClass(DispatcherServlet.class)
    public static class WebMvcServletEndpointManagementContextConfiguration {

        private final ApplicationContext context;

        public WebMvcServletEndpointManagementContextConfiguration(
                ApplicationContext context) {
            this.context = context;
        }

        @Bean
        public ServletEndpointRegistrar servletEndpointRegistrar(
                WebEndpointProperties properties,
                ServletEndpointsSupplier servletEndpointsSupplier) {
            DispatcherServletPath dispatcherServletPath = this.context
                    .getBean(DispatcherServletPath.class);
            return new ServletEndpointRegistrar(
                    dispatcherServletPath.getRelativePath(properties.getBasePath()),
                    servletEndpointsSupplier.getEndpoints());
        }

    }

}

SpringBoot啓動過程this

@SpringBootApplication
public class CrawlerDiscreditApplication {

    public static void main(String[] args) {

        SpringApplication.run(CrawlerDiscreditApplication.class, args);

    }

}

SpringApplication類url

public class SpringApplication {

        private ResourceLoader resourceLoader;

        public SpringApplication(Class<?>... primarySources) {
        this(null, primarySources);
    }

        public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        this.webApplicationType = deduceWebApplicationType();
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = deduceMainApplicationClass();
    }

    
        

}
相關文章
相關標籤/搜索