@Autowired注入空指针

@Autowired注入空指针的状况,我是发生在利用反射来获取到实例化对象之后,调用里面的方法的时候发生的,具体原因我现在理解的不是很清楚,跟mybatis底层注入有关,解决办法如下:


在利用反射来调用方法的类中,注入一个SqlSessionTemplate持久化模板对象,然后在调用方法的时候,将这个模板对象也传进去

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	baseAuditService.addAudit(sqlSessionTemplate,groupId, areaId, Long.parseLong(id), true);


然后在方法中,不通过原来的@Autowired注入来获取对应的mapper对象,而是通过这个持久化模板对象来进行获取

	public boolean addAudit(SqlSessionTemplate sessionTemplate,int groupId, String areaId, long id, boolean audit) {
		
		SqlSession session = sessionTemplate;
		
		BusiTabMapper tabMapper = session.getMapper(BusiTabMapper.class);


通过这种方式来获得的mapper对象,实测是可以运行通过而不报空指针的,后期如果能够解读明白这个问题产生的原因再来进行解析。





猜你喜欢

转载自blog.csdn.net/yili0000a/article/details/75331670