为什么网站经常被攻击,企业网站栏目设计,动漫网站建设的目标,品牌策划经典案例问题再现 兄弟们#xff0c;看见了吗#xff1f;这里我Autowired进来的forkliftService 居然为null 且我SysForkliftServiceImpl上面是加了Service注解的
分析原因
主要原因就是因为该类继承了一个第三方框架SimpleChannelInboundHandler#xff0c;在执行的过程中#…问题再现 兄弟们看见了吗这里我Autowired进来的forkliftService 居然为null 且我SysForkliftServiceImpl上面是加了Service注解的
分析原因
主要原因就是因为该类继承了一个第三方框架SimpleChannelInboundHandler在执行的过程中它是被人家框架内部创建实例然后去调用的这就导致了可能在内部new过这个对象了所以就导致了Component对这个类根本不起作用。
解决办法
创建一个MyBeanUtil
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;Component
public class MyBeanUtil implements ApplicationContextAware {protected static ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext app) throws BeansException {if (applicationContext null) {applicationContext app;}}/*** 通过类的class从容器中手动获取对象*/public static T T getBean(ClassT clazz) {return applicationContext.getBean(clazz);}
}
然后不用 Autowired改用我们的工具类
ISysForkliftService forkliftService MyBeanUtil.getBean(ISysForkliftService.class);这样就能获取到对象了下课