配置文件:
父类:
<bean id="baseServiceSupport" class="com.xpith.struts.action.BaseServiceSupport"
scope="prototype">
<property name="applicationsService">
<ref bean="applicationsService" />
</property>
</bean>
子类:
<bean id="user" class="com.xpith.struts.action.UserAction" scope="prototype"
parent="baseServiceSupport">
<property name="membersService">
<ref bean="membersService" />
</property>
</bean>父类属性:
………………………………………………………………
protected ApplicationsService applicationsService;
…………………………………………………………………
public void setApplicationsService(ApplicationsService applicationsService) {
this.applicationsService = applicationsService;
}子类属性:
public class UserAction extends BaseServiceSupport {
private MembersService membersService;
public void setMembersService(MembersService membersService) {
this.membersService = membersService;
}
}在父类中,抛出applicationsService异常,判断applicationsService == null 为真~~为什么会这样呢?
父类:
<bean id="baseServiceSupport" class="com.xpith.struts.action.BaseServiceSupport"
scope="prototype">
<property name="applicationsService">
<ref bean="applicationsService" />
</property>
</bean>
子类:
<bean id="user" class="com.xpith.struts.action.UserAction" scope="prototype"
parent="baseServiceSupport">
<property name="membersService">
<ref bean="membersService" />
</property>
</bean>父类属性:
………………………………………………………………
protected ApplicationsService applicationsService;
…………………………………………………………………
public void setApplicationsService(ApplicationsService applicationsService) {
this.applicationsService = applicationsService;
}子类属性:
public class UserAction extends BaseServiceSupport {
private MembersService membersService;
public void setMembersService(MembersService membersService) {
this.membersService = membersService;
}
}在父类中,抛出applicationsService异常,判断applicationsService == null 为真~~为什么会这样呢?
有定义吗??
<bean id="applicationsService" class="com.xpith.service.impl.ApplicationsServiceImpl">
<property name="dao">
<ref bean="applicationsDao" />
</property>
</bean>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="son" name="/son" class="Son" abstract="false"
lazy-init="default" autowire="default"
dependency-check="default">
<property name="message">
<value type="java.lang.String">儿子</value>
</property>
</bean>
<bean id="father" name="/father" class="Father" abstract="false"
lazy-init="default" autowire="default"
dependency-check="default">
<property name="child">
<ref bean="son" /> <!--在父类配置中注入子类-->
</property>
<property name="message">
<value type="java.lang.String">父亲</value>
</property>
</bean>
</beans>
具体是哪个文件报错,Action?如果是的话贴出Action的代码来看看
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [applicationContext-test.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.xpith.struts.action.UserAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at com.xpith.struts.test.UserAction.registerTest(UserAction.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.xpith.struts.action.UserAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
... 23 more
Caused by: java.lang.NullPointerException
at com.xpith.struts.action.BaseServiceSupport.initParams(BaseServiceSupport.java:66)
at com.xpith.struts.action.BaseServiceSupport.<init>(BaseServiceSupport.java:39)
at com.xpith.struts.action.UserAction.<init>(UserAction.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
... 25 more
package com.xpith.struts.action;import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xpith.orm.Applications;
import com.xpith.service.ApplicationsService;
import com.xpith.util.Coder;
import com.xpith.util.SecurityHelper;
import com.xpith.util.StringFiltrateHelper;public class BaseServiceSupport extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1972820142271999927L; protected HashMap<String, String> params = new HashMap<String, String>();
// protected HttpServletResponse response =
// ServletActionContext.getResponse();
protected ActionContext context = ActionContext.getContext(); // These property is not init.It will be implemented later.
protected ApplicationsService applicationsService;
protected Applications app; public BaseServiceSupport() { initParams();
} protected void writeRespond(String str) { PrintWriter pw;
// pw = response.getWriter();
pw = new PrintWriter(System.out);
pw.print(str);
pw.flush();
pw.close();
} private void initParams() { // Map<String, Object> map = context.getParameters();
// Iterator<Map.Entry<String, Object>> p = map.entrySet().iterator();
// String str = p.next().getKey().toString();
//
// StringFiltrateHelper.splitParams(params, str); params.put("appid", "1"); int appid = Integer.parseInt(params.get("appid"));
if (applicationsService == null) {
System.out.println("fuck");
}
app = applicationsService.getApplicationsById(appid); String authkey = app.getAuthkey(); String input = params.get("input");
if (input != null && input.length() > 0) {
input = StringFiltrateHelper.urldecode(input);
params.remove("input");
try {
byte[] code = Coder.decryptBASE64(input);
input = SecurityHelper.authDecode2(code, authkey);
StringFiltrateHelper.splitParams(params, input);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public ApplicationsService getApplicationsService() {
return applicationsService;
} public void setApplicationsService(ApplicationsService applicationsService) {
this.applicationsService = applicationsService;
} public HashMap<String, String> getParams() {
return params;
} public void setParams(HashMap<String, String> params) {
this.params = params;
}}
你试着给父类创建一个空的构造方法.应该会成功,spring还可以设置bean创建后执行的init方法. 如果你要设置init方法,可以设置一下.
这个方法中又涉及到还未注入的属性.所以抛出空指针了
结贴了,不知道还有没有别的办法在基类初始化的时候执行一些操作,就像init~~