解决A component required a bean of type 'javax.activation.DataSource' that could not be found问题

问题描述

Spring Boot启动报错信息:

2020-01-27 23:29:19.837  WARN 1514 --- [           main] o.s.boot.StartupInfoLogger               : InetAddress.getLocalHost().getHostName() took 5004 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2020-01-27 23:29:24.845  INFO 1514 --- [           main] c.z.s.SpringbootLearningApplication      : Starting SpringbootLearningApplication on Yitian-MacBook-Pro.local with PID 1514 (/Users/yitian/Documents/IDEAWorkspaces/LocalProjects/springboot-learning/target/classes started by yitian in /Users/yitian/Documents/IDEAWorkspaces/LocalProjects/springboot-learning)
2020-01-27 23:29:24.846  INFO 1514 --- [           main] c.z.s.SpringbootLearningApplication      : No active profile set, falling back to default profiles: default
2020-01-27 23:29:25.691  INFO 1514 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-27 23:29:25.699  INFO 1514 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-27 23:29:25.699  INFO 1514 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-01-27 23:29:25.752  INFO 1514 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-27 23:29:25.752  INFO 1514 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 869 ms
2020-01-27 23:29:25.817  WARN 1514 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceShow' defined in file [/Users/yitian/Documents/IDEAWorkspaces/LocalProjects/springboot-learning/target/classes/cn/zyt/springbootlearning/tools/DataSourceShow.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.activation.DataSource' available
2020-01-27 23:29:25.819  INFO 1514 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-01-27 23:29:25.831  INFO 1514 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-27 23:29:25.902 ERROR 1514 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'javax.activation.DataSource' that could not be found.


Action:

Consider defining a bean of type 'javax.activation.DataSource' in your configuration.

相应的源码:

/**
 * 监测数据库连接池类型
 *
 * 实现Spring Bean生命周期接口ApplicationContextAware
 */
@Component
public class DataSourceShow implements ApplicationContextAware {
    private ApplicationContext applicationContext = null;

    /**
     * Spring容器会自动调用这个方法,注入到Spring IOC容器中
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        System.out.println("--------------------------------");
        System.out.println(dataSource.getClass().getName());
        System.out.println("--------------------------------");
    }
}

问题解决

该问题的原因为源码中DataSource的引入包错误,不应该是'javax.activation.DataSource',而是:

import javax.sql.DataSource;
发布了296 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/yitian_z/article/details/104098175