日期:2014-05-20  浏览次数:20903 次

Spring 注入问题
先说下我的类的结构:

abstract BaseService: 所有业务逻辑操作类的父类
LoginService: 具体的登陆操作,继承BaseService


BaseAction : 所有Action的父类,该类有一个 BaseService 的引用,叫 service
LoginAction: 具体的登陆Action,每个Action 可能调用一个响应的 Service,这个 Action 调用 LoginService;

然后用 Spring 注入:

Java code

<!-- Service -->
<bean id="login" class="com.qiaogu.service.anonymous.LoginService" scope="prototype" autowire="byName"/>

<!-- Action -->
<bean id="loginAction" scope="prototype" class="com.qiaogu.action.anonymous.LoginAction">
    <property name="service" ref="login" />
</bean>



Action 里有一个 getService 方法,用来把 BaseService 类型的 service 对象转换成相应的 service(LoginService)
Java code

public LoginService getService(){
    return (LoginService)service;
}



Struts.xml 配置是正确的,然后我运行的时候,出现以下错误:

Java code

Unable to instantiate Action, loginAction,  defined for 'login' in namespace '/'Error creating bean with name 'loginAction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.qiaogu.service.anonymous.LoginService] to required type [com.qiaogu.service.anonymous.LoginService] for property 'service'; nested exception is java.lang.IllegalArgumentException: Original must not be null
    com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:306)
    com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:387)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

更多底层异常省略 ...



求各位帮帮忙,晚上通宵整一晚,都不晓得哪里有问题 ...

------解决方案--------------------
XML code

<!-- Service -->
<bean id="loginService" class="com.qiaogu.service.anonymous.LoginService" scope="prototype" autowire="byName"/>

<!-- Action -->
<bean id="loginAction" scope="prototype" class="com.qiaogu.action.anonymous.LoginAction">
    <property name="loginService" ref="loginService" />
</bean>