天天看點

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){


    }