spring注入jdbcTemplate时一直为空指针null

博主在多数据源项目中,第二个数据源采用spring的jdbcTemplate,有一个项目的jdbcTemplate启动后为null。启动时该对象有数据,之后变为null。博主通过网上查找,找到解决方案,即在jdbcTemplate对象上加注解@Resource(name = \jdbcTemplate\)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天做一个项目,多数据源,第二个数据源采用的是spring的jdbcTemplate。三个项目,两个都没问题,其中一个jdbcTemplate一直为null。检查了一下,在spring启动时,jdbcTemplate对象也是有数据的,就是不知道为啥启动好之后,就变成null了,很头疼。后来去网上找了一下,发现其中一种解决方案可以解决我的问题。

先上我有问题的代码吧。

1、数据库配置(app-smsJdbc.xml):

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- IOC和DI的注解扫描 -->
    <context:component-scan base-package="cn.gtmap.estateplat.register.service.smsImpl" ></context:component-scan>

    <!-- 打开AOP的注解 -->
    <!-- 这里用的是中间的横线而不是下划线 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass" value="${sms.db.driver}"></property>
        <property name="jdbcUrl" value="${sms.db.url}"></property>
        <property name="user" value="${sms.db.username}"></property>
        <property name="password" value="${sms.db.password}"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="cmccSmsServiceImpl" class="cn.gtmap.estateplat.register.service.smsImpl.CmccSmsServiceImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

</beans>

2、Java代码(CmccSmsServiceImpl.java):

package cn.gtmap.estateplat.register.service.smsImpl;

import cn.gtmap.estateplat.core.ex.AppException;
import cn.gtmap.estateplat.register.service.SmsService;
import cn.gtmap.estateplat.utils.CalendarUtil;
import com.gtis.common.util.UUIDGenerator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("cmccSmsServiceImpl")
public class CmccSmsServiceImpl implements SmsService {

    private static final String PHONE_NUMBER_NULL = "电话号码为空!";
    private static final String CONTENT_NUMBER_NULL = "短信内容为空!";
    private static final String REQDELIVERYREPORT_NO_NUMBER = "需要状态报告不是数字!";
    private static final String MSGFMT_NO_NUMBER = "消息类型不是数字!";
    private static final String SENDMETHOD_NO_NUMBER = "短信发送形式不是数字!";
    private String extCode = "**";
    private String reqDeliveryReport = "*";
    private String msgFmt = "*";
    private String sendMethod = "*";
    private String applicationId = "**********";

    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void sendSms(HashMap hashMap, String model, String phone) {
        List<String> phoneList = new ArrayList<String>();
        phoneList.add(phone);
        if (CollectionUtils.isEmpty(phoneList)) {
            throw new AppException("电话号码为空!");
        } else if (StringUtils.isBlank(model)) {
            throw new AppException("短信内容为空!");
        } else if (!StringUtils.isNumeric(this.reqDeliveryReport)) {
            throw new AppException("需要状态报告不是数字!");
        } else if (!StringUtils.isNumeric(this.msgFmt)) {
            throw new AppException("消息类型不是数字!");
        } else if (!StringUtils.isNumeric(this.sendMethod)) {
            throw new AppException("短信发送形式不是数字!");
        } else {
            String sql = "INSERT INTO **** (SISMSID, EXTCODE, DESTADDR,MESSAGECONTENT, REQDELIVERYREPORT, MSGFMT, SENDMETHOD, REQUESTTIME, APPLICATIONID) VALUES (?,?,?,?,?,?,?,?,?)";
           // for(String phoneNum:phoneList){
                try {
                    Object[] param = new Object[]{UUIDGenerator.generate18(), this.extCode, phone, model, Integer.parseInt(this.reqDeliveryReport), Integer.parseInt(this.msgFmt), Integer.parseInt(this.sendMethod), CalendarUtil.getCurHMSDate(), this.applicationId};
                    jdbcTemplate.update(sql, param);
                } catch (Exception var5) {
                    throw new AppException(var5.getMessage());
                }
        //    }
    //        return null;
        }
    }
}

3、项目启动时,这个jdbcTemplate对象还是有数据的:

但是,项目启动成功后,再做业务,会发现jdbcTemplate对象为空了:

是不是很尴尬。

 

后来看了网上一些解决办法,找到了如下办法,在jdbcTemplate对象是加上注解:

 @Resource(name = "jdbcTemplate"),上代码(CmccSmsServiceImpl.java):

package cn.gtmap.estateplat.register.service.smsImpl;

import cn.gtmap.estateplat.core.ex.AppException;
import cn.gtmap.estateplat.register.service.SmsService;
import cn.gtmap.estateplat.utils.CalendarUtil;
import com.gtis.common.util.UUIDGenerator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("cmccSmsServiceImpl")
public class CmccSmsServiceImpl implements SmsService {

    private static final String PHONE_NUMBER_NULL = "电话号码为空!";
    private static final String CONTENT_NUMBER_NULL = "短信内容为空!";
    private static final String REQDELIVERYREPORT_NO_NUMBER = "需要状态报告不是数字!";
    private static final String MSGFMT_NO_NUMBER = "消息类型不是数字!";
    private static final String SENDMETHOD_NO_NUMBER = "短信发送形式不是数字!";
    private String extCode = "**";
    private String reqDeliveryReport = "*";
    private String msgFmt = "*";
    private String sendMethod = "*";
    private String applicationId = "**********";
    @Resource(name = "jdbcTemplate")//注意看这里!!!!!!!!!!!!!!!!!!!!
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void sendSms(HashMap hashMap, String model, String phone) {
        List<String> phoneList = new ArrayList<String>();
        phoneList.add(phone);
        if (CollectionUtils.isEmpty(phoneList)) {
            throw new AppException("电话号码为空!");
        } else if (StringUtils.isBlank(model)) {
            throw new AppException("短信内容为空!");
        } else if (!StringUtils.isNumeric(this.reqDeliveryReport)) {
            throw new AppException("需要状态报告不是数字!");
        } else if (!StringUtils.isNumeric(this.msgFmt)) {
            throw new AppException("消息类型不是数字!");
        } else if (!StringUtils.isNumeric(this.sendMethod)) {
            throw new AppException("短信发送形式不是数字!");
        } else {
            String sql = "INSERT INTO **** (SISMSID, EXTCODE, DESTADDR,MESSAGECONTENT, REQDELIVERYREPORT, MSGFMT, SENDMETHOD, REQUESTTIME, APPLICATIONID) VALUES (?,?,?,?,?,?,?,?,?)";
           // for(String phoneNum:phoneList){
                try {
                    Object[] param = new Object[]{UUIDGenerator.generate18(), this.extCode, phone, model, Integer.parseInt(this.reqDeliveryReport), Integer.parseInt(this.msgFmt), Integer.parseInt(this.sendMethod), CalendarUtil.getCurHMSDate(), this.applicationId};
                    jdbcTemplate.update(sql, param);
                } catch (Exception var5) {
                    throw new AppException(var5.getMessage());
                }
        //    }
    //        return null;
        }
    }
}

 

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值