天天看点

Batch定义Writer出现Could not autowire. There is more than one bean of ‘DataSource‘ type

Batch定义Writer出现Could not autowire. There is more than one bean of ‘DataSource‘ type

按照https://blog.csdn.net/qq_40929047/article/details/89522718%20s的文章中定义Writer对象时,尝试将其加入SpringIoc容器出现爆红。

这种现象是因为SpringBoot本身就已经将DataSource给加入到ioc容器里头了,也就是DataSource本身就已经被创建为了一个Bean。

出现这种情况有两种办法,第一是使用@Qualifier来指定,@Qualifier的用法参考:

https://blog.csdn.net/qq_36567005/article/details/80611139

public ItemWriter<Person> writer(DataSource dataSource){


    }
           

一种是用本身就已经定义好的Bean,那么久去掉Bean注解即可。

@Bean
    public ItemWriter<Person> writer(@Qualifier("dataSource") DataSource dataSource){


    }