问题描述
1. 在类一加载的时候就执行连接方法,且只执行一次-->init方法
2. Plc接口的两个实现类为了实现代码的复用,init方法中要把注入的实现类传过去
3. 矛盾:init方法的执行在spring加载bean之前
结果:会导致执行init方法时,实现类注入的为null
解决思路:把init方法注入进来
// Plc的实现类
@Resource(name = "ModbusTcp")
PlcInterface plcInterface;
// 操作plc的公用方法
PlcMethods plcMethods;
@Autowired
public void init(PlcMethods plcMethods){
plcMethods.getConnect(IP_ADDRESS,PORT,plcInterface);
this.plcMethods = plcMethods;
}