问题描述:
1. 问题:
在类中需要引入Mapper持久层对象,完成数据库CRUD操作,但是岁引入的对象为null,引入失败!
---->如图-"图-1"+“图-2”
图-1
图-2
原因分析:
类无法引入,@Autowired注解注入失败。或所要引入的类没有实现将组件的形式注册到spring容器中。
解决方案:
1. 我们可以用setter的方式将对象的实例赋予给对象,使用@PostConstruct注解赋予该类的静态对象一个实例,从而真正实例化静态对象。同时遵循有点:
第一点:此类应使用@component这个注解,代表这个类以组件的形式注册到spring容器中,从而能够被容器管理;
第二点:通过setter方式实例化静态对象时,另一个赋值的对象应当使用@Autowired注解进行引入。
2. 代码调整为,如下所示:
@Component
public class FIXSender implements Application {
private static ExchangeOrderMapper staticexchangeOrderMapper;
@Autowired
private ExchangeOrderMapper exchangeOrderMapper;
@PostConstruct
private void initStaticMapper() {
staticexchangeOrderMapper = this.exchangeOrderMapper;
}
public void updateMessage(List<String> list) {
String clordid = "";
String errorValue = "";
ExecuteRequest request = new ExecuteRequest();
for (int i = 0; i < list.size(); i++) {
String result = list.get(i);
String[] split = result.split("=");
String s = split[0];
System.out.println("s--------------------->" + s);
if (s.equals("11")) {
clordid = split[1];
}
if (split[0].equals("58")) {
errorValue = split[1];
}
if (clordid != "" && errorValue !="") {
request.setClzt(-1);
request.setWbfhdm("58");
request.setWbfhxx(errorValue);
request.setClordID(clordid);
staticexchangeOrderMapper.exchangeExecute(request);
}
}
}
}
◆:此时就可以实现'ExchangeOrderMapper '接口正常引入,并可以
使用此接口底下的方法。