java jdbctemplate. 老是null_spring注入jdbcTemplate时一直为空指针null-Go语言中文社区...

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

先上我有问题的代码吧。

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

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">

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 phoneList = new ArrayList();

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对象还是有数据的:

b9f1f99caf84c00a4dc8ebd1ea0bf34a.png

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

ffc9cfc671892a37acd0219edfe01970.png

是不是很尴尬。

后来看了网上一些解决办法,找到了如下办法,在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 phoneList = new ArrayList();

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>