消息队列监听器无法注入bean的问题解决
无论是srping配置文件中的bean,注解中的bean,dubbo注解中的bean到了spring之后都会到spring的上下文,
dubbo注解bean有时获取不到需要在customer另外用配置的方式即可(两套上下文问题)
activeMq,rabbitMq这些用监听形式配置的bean会先于dispatch.xml中的bean扫描,所以直接用@Autwire会获取不到dispatch.xml中的bean,
此时需要显示从上下文获取(初始化时机问题)
public class BeanUtil implements ApplicationContextAware {//实现这个接口可以自动注入spring的上下文
private static WebApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = (WebApplicationContext) applicationContext;
/* ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
*/ }
}
package com.houbank.incoming.web.rabbitMq;
import java.util.Date;
import com.alibaba.dubbo.config.annotation.Reference;
import com.houbank.incoming.api.FinancialSalesSMSMqMessageFacade;
import com.houbank.incoming.model.domain.SMSMqMessage;
import com.houbank.incoming.web.controller.FinancialInterfaceAccountInfoController;
import com.houbank.incoming.web.session.BeanUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
@Controller
public class XSApplyIdListener {
private Logger logger = LoggerFactory.getLogger(XSApplyIdListener.class);
@Reference
private FinancialSalesSMSMqMessageFacade financialSalesSMSMqMessageFacade;
public void listen(Object message) {
// {"pass":true, "orderNo": "11111"} {"pass":false, "orderNo": "11111", "failReason": "身份证不对"} {"pass":false, "orderNo": "11111", "failReason": "[0000],电销微信交单失败"}
//微信交单状态,0:不符合进件标准;1:待处理,2待客户补充材料,3已提交待复核;4:已提交待复核信审接收成功 5:已提交待复核信审接收失败 6:已提交待复核信审接收失败【可以重复提交信审】
try {
financialSalesSMSMqMessageFacade = (FinancialSalesSMSMqMessageFacade)BeanUtil.getBean("financialSalesSMSMqMessageFacade");
BeanUtil.getBean("restTemplate1");
logger.info("receive message: " + message); // jSONObjectReq = JSONObject.fromObject(jsonReq.trim());
String message1 = new String(((byte[])message), "UTF-8");
SMSMqMessage xinshenReturnMessage = (SMSMqMessage)JSONObject.toBean(JSONObject.fromObject(message1), SMSMqMessage.class);
System.out.print("============jyhjdtyjdy================="+xinshenReturnMessage);
if(xinshenReturnMessage == null){
return;
}
financialSalesSMSMqMessageFacade.insert(xinshenReturnMessage);
// String orderNo = xinshenReturnMessage.get("orderNo") == null ? "" : xinshenReturnMessage.getString("orderNo");
} catch (Exception e) {
logger.error(e.getMessage());
return ;
}
}
}
参考:
https://blog.csdn.net/gaoshili001/article/details/77776863