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

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有很多依赖项中有很多同名,但不同包的类型,所以使用时要注意

原文地址:https://www.cnblogs.com/myboat/p/11493215.html