Parameter 0 of constructor in work.dao.impl.SqlDaoImpl required a bean of type 'org.springframework.

Parameter 0 of constructor in work.dao.impl.SqlDaoImpl required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
- Bean method 'jdbcTemplate' in 'JdbcTemplateAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) found beans 'jdbcTemplateOnlyRead', 'jdbcTemplateAdminSelf', 'jdbcTemplateHistoryWrite', 'jdbcTemplateWriteMysql', 'jdbcTemplateRead', 'jdbcTemplateReadMysql', 'jdbcTemplateHistoryRead', 'jdbcTemplateWrite'

JdbcTemplateAutoConfiguration中的方法jdbcTemplate加载失败,找到JdbcTemplateAutoConfiguration这个类:

@Configuration
@ConditionalOnClass({DataSource.class, JdbcTemplate.class})
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class JdbcTemplateAutoConfiguration {
    private final DataSource dataSource;

    public JdbcTemplateAutoConfiguration(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean({JdbcOperations.class})
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(this.dataSource);
    }

会发现在jdbcTemplate方法上有个@ConditionalOnMissingBean的注释,这个注释其实说的是当存在JdbcOperations这个类的对象时就跳过这个方法,所以会造成jdbcTemplate加载失败.

原因是因为在applicationContext.xml中配置了

<bean id="jdbcTemplateWrite"  class="org.springframework.jdbc.core.JdbcTemplate">
<qualifier value="dataSourceWrite"/>
</bean>

去掉这个bean,重新再其他地方进行配置org.springframework.jdbc.core.JdbcTemplate,比如我自己写了一个加载配置文件的类,并在配置类中进行配置

@Configuration
@ImportResource("classpath:application-context.xml")
public class ConfigClass {
    @Bean("jdbcTemplateWrite")
    public JdbcTemplate jdbcTemplateWrite(@Qualifier("dataSourceWrite")DataSource dataSource){
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

就可以解决这个问题了。.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值