普通类中使用@Autowired等注入会失败,常见的注入:
Controller层中使用@Controller将类加入容器,再自动注入Service层的实例;
Service层使用@Service将类加入到容器,再自动注入DAO层的实例;
DAO层使用@Mapper将类加入到容器中,或者使用@MapperScan将dao包下类扫描在spring容器中进行注册。
解决方法:
@Component
public class CustomProtocolDecoder extends CumulativeProtocolDecoder {
@Autowired
private MachineIpBlacklistService machineIpBlacklistService;
private static CustomProtocolDecoder customProtocolDecoder;
// 通过@PostConstruct实现初始化bean之前进行的操作
@PostConstruct
public void init() {
customProtocolDecoder = this;
// 测试发现不写此段代码也可以使用,但不知道为什么,希望大神可以解惑。
customProtocolDecoder.machineIpBlacklistService = this.machineIpBlacklistService;
}
// 测试调用
customProtocolDecoder.machineIpBlacklistService.<你的service层方法>;
}
26万+

被折叠的 条评论
为什么被折叠?



