jar中autowired字段的抽象类bean定义

时间:2014-03-31 21:09:28

标签: spring javabeans abstract extend autowired

我遇到了自动装配问题。这是我项目的情况:

我有一个罐子(让我们称之为 module.jar )。这个jar包含一个接口( IService ):

package com.module.service;

public interface IService {

    String create(Object data) throws Exception;
    String update(Object data);
}

实现接口的抽象类( ServiceImpl ):

package com.module.service

public abstract class ServiceImpl implements IService {

    @Override
    public final String create(Object data) throws Exception {
        return update(data);
    }

    public abstract String update(Object data);
}

和一个单独的类,其中包含一个自动装配的 IService 字段:

package com.module.service;

public class DoSomething {

    @Autowired
    IService service;

    public String doCreate(Object data) {
        return service.create(data);
    }
}

我有一个项目(我们称之为项目)。这个项目包含 module.jar ,以及一个扩展抽象 ServiceImpl 的类(我们称之为 ServiceImplExt )。 ServiceImplExt 的声明如下:

package com.project.service;

import com.module.service.ServiceImpl;

@Service
public class ServiceImplExt extends ServiceImpl {

    @Override
    public String update(Object data) {
        data.setFname("John");
        data.setLname("Doe");
        return data;
    }   
}

我已将 ServiceImplExt 的软件包添加到我的spring xml的组件扫描中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.project.service" />

</beans>

毕竟,在我启动服务器时,我得到以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.demo.service.IService] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 42 more

我无法弄清楚我做错了什么。有什么想法吗?


添加了代码,为辅助潜在帮助者添加了xml

0 个答案:

没有答案
相关问题