Spring父类属性无法注入

Yu_track 2011-05-17 11:35:42
配置文件:
父类:
<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 为真~~为什么会这样呢?
...全文
980 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
<ref bean="applicationsService" />
有定义吗??
qianmz 2011-05-17
  • 打赏
  • 举报
回复
没用看到bean="applicationsService"的定义嘛??
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
求助啊·~
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
spring 可以设置初始化之后执行init方法,init方法可以设置.
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 lianghaijian 的回复:]
要么你就使用spring的构造方法注入.
你试着给父类创建一个空的构造方法.应该会成功,spring还可以设置bean创建后执行的init方法. 如果你要设置init方法,可以设置一下.
[/Quote]

结贴了,不知道还有没有别的办法在基类初始化的时候执行一些操作,就像init~~
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
原来,基类调用构造函数的时候,spring还没注入~结果导致了悲剧~以后还要多多熟悉spring~
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
终于明白了,郁闷了一个下午。~~谢谢大家~~
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
spring基于setter的注入,是在bean被创建之后再做的.而你在创建的同时,调用了initParams();
这个方法中又涉及到还未注入的属性.所以抛出空指针了
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
再试试~
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
那个空指针异常,应该是因为还没注入那个属性,你就调用那个属性了.而不是注入不成功的原因
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
你现在先测试一下,创建一个空的构造方法,用子类注入.应该可以的.
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
要么你就使用spring的构造方法注入.
你试着给父类创建一个空的构造方法.应该会成功,spring还可以设置bean创建后执行的init方法. 如果你要设置init方法,可以设置一下.
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
完整的父类代码:(测试时注释掉了一些功能)
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;
}

}
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 lianghaijian 的回复:]

之前我也碰到如此问题.我回去翻了一下源码.之前在一个教学视频也看到过这个问题,解决办法有好几个,这边说一个

既然你父类的那个属性设置为protect,那么在子类中设置setter和getter方法,在子类中注入也是可以的. 我就是这样做的.

你可以试一下
[/Quote]

好像,在子类里面注入都不行。。真不到为什么了。。
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
好的,谢谢,我试试~~
危险的大猫 2011-05-17
  • 打赏
  • 举报
回复
之前我也碰到如此问题.我回去翻了一下源码.之前在一个教学视频也看到过这个问题,解决办法有好几个,这边说一个

既然你父类的那个属性设置为protect,那么在子类中设置setter和getter方法,在子类中注入也是可以的. 我就是这样做的.

你可以试一下
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wl_ldy 的回复:]

"在父类中,抛出applicationsService异常"
具体是哪个文件报错,Action?如果是的话贴出Action的代码来看看。。。
[/Quote]

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

wl_ldy 2011-05-17
  • 打赏
  • 举报
回复
"在父类中,抛出applicationsService异常"
具体是哪个文件报错,Action?如果是的话贴出Action的代码来看看。。。
kyousuke 2011-05-17
  • 打赏
  • 举报
回复
为啥都不喜欢用注解呀,求解释
Yu_track 2011-05-17
  • 打赏
  • 举报
回复
继续求助,我弄了一下午,还是没解决这个问题。不知道为什么。
加载更多回复(7)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧