ssh做项目,总是报空指针不能进入userservice,调试,userservice为null,
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh做项目,总是报空指针不能进入userservice,调试,userservice为null,相关的知识,希望对你有一定的参考价值。
ssh做项目,总是报空指针不能进入userservice,调试,userservice为null,网上的解法,一种是看是否userservice已经getset,这步做了,再一种是把struts配置的action的bean class改为spring的bean id,但是改成spring的bean id,直接跑不动了,报action的classnotfound
参考技术A 1.service为null的情况一般有几种情况:spring的配置文件没有配置;依赖注入由于写的不仔细而没有实现注入等原因。首先检查是否在spring的配置文件中注入了你想要的service如果有,检查是否因为大小写的小错误而不能实现注入。2.可以在action的这样处理:
让action继承一个自己编写的BaseAction 在让BaseAction去继承DispatchAction 在BaseAction中实现service的获取
public class BaseAction extends DispatchAction
protected Object getBean(String id)
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.servlet.getServletContext());
return context.getBean(id);
然后在你的action中就可以直接通过方法得到你想要的service。
IService service = (IService) getBean("你想要的service");
你就可以用service调用你的CRUD方法执行了!
这里主要用到的是spring的DI(依赖注入)spring的核心之一! 参考技术B 这样说很难判断啊
我的构造函数里面报空指针异常了!求大神帮忙
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in class path resource [spring/applicationContext-shiro.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in class path resource [spring/applicationContext-shiro.xml]: Cannot resolve reference to bean 'customRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customRealm': Unsatisfied dependency expressed through field 'imUserService': Error creating bean with name 'imUserService' defined in class path resource [spring/applicationContext-service.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.sinosoft.sinoims.service.impl.ImUserServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imUserService' defined in class path resource [spring/applicationContext-service.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.sinosoft.sinoims.service.impl.ImUserServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
Caused by: java.lang.NullPointerException
at com.sinosoft.sinoims.util.SpringUtils.getBean(SpringUtils.java:44)
at com.sinosoft.sinoims.service.impl.ImUserServiceImpl.<init>(ImUserServiceImpl.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 94 more
厉害啦,问题真实这样,已解决!
参考技术A 我,鲁智深,无话可说追问???
以上是关于ssh做项目,总是报空指针不能进入userservice,调试,userservice为null,的主要内容,如果未能解决你的问题,请参考以下文章
getActionBar().setDisplayHomeAsUpEnabled(true)报空指针(已解决)