求Could not autowire. No beans of 'DataSource' type found.解决方法

img

换成全注释反而报错

img

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test.SpringJuitTest': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

检查下你注入的有DataSouce的bean吗
报这个错是因为在spring ioc容器里找不到对应的bean
@Autowired引入的bean都要注入在ioc里才能获取

写个配置类:

@Configuration
public class Config {

    @Bean
    public DataSource getDataSource() {
        return new DruidDataSource();
    }
}