纠结一夜没睡,好困,spring mvc注入抽象方法的子类注册不成功

catch不住的爱 2013-11-07 10:25:41
本人用spring mvc写了一个项目,现有一个抽象类,两个抽象类的子类,想在controller中注入一个抽象类的实现类,可是老是提示我的父类没有注册
直接上图了

配置文件
<!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
<mvc:resources mapping="/img/**" location="/imgage/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/html/**" location="/html/"/>

<!--
①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能
-->
<context:annotation-config/>
<context:component-scan base-package="com.honestspring.controller"></context:component-scan>
<context:component-scan base-package="com.honestspring.dao"></context:component-scan>
<context:component-scan base-package="com.honestspring.service"></context:component-scan>
<context:component-scan base-package="com.honestspring.sys.component"></context:component-scan>
<context:component-scan base-package="com.honestspring.sys.tld"></context:component-scan>
<context:component-scan base-package="com.honestspring.model"></context:component-scan>


调用的地方
	@Autowired
@Qualifier("hSListViewComponent")
public AbtractSysViewComponent hsViewComponent;

其中 AbstractSysViewComponent类
import com.honestspring.util.XMLManager;

public abstract class AbtractSysViewComponent {
public abstract String spellFileName(String module);

public Document getResource(String module) {

实现类
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.honestspring.common.Constant;
import com.honestspring.sys.model.DataGrid;
import com.honestspring.sys.screen.model.Column;
import com.honestspring.sys.screen.model.Function;
import com.honestspring.sys.screen.model.Toolbar;
import com.honestspring.util.DataGridModel;
import com.honestspring.util.XMLManager;

@Component
public class HSListViewComponent extends AbtractSysViewComponent {
@Autowired
private FieldWidthOfListStrategyIntf fieldWidthOfListStragey;


报错信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'listViewAjaxController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.honestspring.sys.component.AbtractSysViewComponent com.honestspring.controller.ListViewAjaxController.hsViewComponent; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.honestspring.sys.component.AbtractSysViewComponent] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=hSListViewComponent)}


个人觉得这个抽象的父类是不需要注册的啊,希望高手指点,好让我早点睡觉
...全文
546 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
朱超ZhuChao.Tech 2013-11-09
  • 打赏
  • 举报
回复
spring mvc 不是有DEMO 的嘛
catch不住的爱 2013-11-09
  • 打赏
  • 举报
回复
要沉了,可是我还没有睡,help,sos
catch不住的爱 2013-11-09
  • 打赏
  • 举报
回复
引用 11 楼 spiniper 的回复:
HSListViewComponent 类名字的问题,对于默认注入类名,首字母会小写,但是当第二个也是大写的时候,就会出现问题,你可以更改类名变成HslistViewComponent 这样或许会注入成功,或者你显示提供bean名称 @Component("hSListViewComponent")这种方式,然后使用Resource(name="hSListViewComponent")完成注入,应该会成功。 另外,我习惯用通用的Resource Autowrite不经常用
太感谢了!
jinshaojie2011 2013-11-09
  • 打赏
  • 举报
回复
引用 11 楼 spiniper 的回复:
HSListViewComponent 类名字的问题,对于默认注入类名,首字母会小写,但是当第二个也是大写的时候,就会出现问题,你可以更改类名变成HslistViewComponent 这样或许会注入成功,或者你显示提供bean名称 @Component("hSListViewComponent")这种方式,然后使用Resource(name="hSListViewComponent")完成注入,应该会成功。 另外,我习惯用通用的Resource Autowrite不经常用
正解!!!
树成 2013-11-09
  • 打赏
  • 举报
回复
HSListViewComponent 类名字的问题,对于默认注入类名,首字母会小写,但是当第二个也是大写的时候,就会出现问题,你可以更改类名变成HslistViewComponent 这样或许会注入成功,或者你显示提供bean名称 @Component("hSListViewComponent")这种方式,然后使用Resource(name="hSListViewComponent")完成注入,应该会成功。 另外,我习惯用通用的Resource Autowrite不经常用
catch不住的爱 2013-11-08
  • 打赏
  • 举报
回复
引用 4 楼 songbgi 的回复:
贴个报错信息呗
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'listViewAjaxController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.honestspring.sys.component.AbtractSysViewComponent com.honestspring.controller.ListViewAjaxController.hsViewComponent; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.honestspring.sys.component.AbtractSysViewComponent] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=hSListViewComponent)}
etfired 2013-11-08
  • 打赏
  • 举报
回复
打错了,注入的是子类,父类没有建立索引
etfired 2013-11-08
  • 打赏
  • 举报
回复
使用@Autowried注解,SPring实现根据类型从上下文中查询,因为注入的是子类,所以上下文中定义的类型为父类,子类并没有建立索引,如果非要这么做,请使用@Resource注解,使用BeanName注入
观鱼塘主 2013-11-08
  • 打赏
  • 举报
回复
有可能是你用父类的时候,程序无法确定用哪个子类来实现注入导致的。
  • 打赏
  • 举报
回复
贴个报错信息呗
catch不住的爱 2013-11-08
  • 打赏
  • 举报
回复
引用 1 楼 ltp2010 的回复:
既然不能注入父类,就先别注入父类,注入子类试试行不行。
子类注册时没问题的
随便5个字 2013-11-07
  • 打赏
  • 举报
回复
既然不能注入父类,就先别注入父类,注入子类试试行不行。

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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