STS中不同包但相同类名引起的问题:A component required a bean of type 'javax.activation.DataSource' that could not be found

匆匆过客 提交于 2019-12-09 10:16:28

1. 问题输出:

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.

2. 源码:

package boat.db.db;

import javax.activation.DataSource
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class DataSourceShow implements ApplicationContextAware {
@Autowired
ApplicationContext applicationContext = null;
@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

改了后,正常运行

总结:

java有很多依赖项中有很多同名,但不同包的类型,所以使用时要注意

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!