• spring使用JdbcTemplate问题记录


    报错

    之前有遇到使用spring的JdbcTemplate对数据库进行操作,但是部署到服务器上的时候就报错了,如下:

    weblogic.application.ModuleException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}:org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

    经过检查发现是JdbcTemplate没有注入。知道原因就很简单了

    解决

    JdbcTemplate实例化bean的方式有很多种,下面只写我使用的方式:

    java代码

    public class JDBCTemplateQueryServiceImpl implements JDBCTemplateQueryService {
    
        private JdbcTemplate jdbcTemplate;
    
        public JdbcTemplate getJdbcTemplate() {
            return jdbcTemplate;
        }
    
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        ...
    }
    
    

    spring的xml配置

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name = "dataSource" ref="dataSource"/>
        </bean>
        <bean id="JDBCTemplateQueryService" class="com.xxx.xxx.JDBCTemplateQueryServiceImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"/>
        </bean>
    
  • 相关阅读:
    CSS箭头
    rails 路由正则表达式
    centos6.7 配置MongoDB日志
    centos6.7 配置Elasticsearch
    拼音纠错
    Pandas学习笔记
    WebService using Spring throwed org.xml.sax.SAXException: Bad envelope tag: htm
    怎样让Windows任务管理器CPU占用率呈现正玄曲线(解释+C#实现)
    Deepin Linux获得关注,国产GUI值得提倡
    Swing组件的另类嵌套
  • 原文地址:https://www.cnblogs.com/ghostwolf1/p/14119556.html
Copyright © 2020-2023  润新知