Java 为什么在尝试将存储库注入控制器类时会出现此运行时错误?

Java 为什么在尝试将存储库注入控制器类时会出现此运行时错误?,java,spring,spring-mvc,annotations,autowired,Java,Spring,Spring Mvc,Annotations,Autowired,使用@Autowired注释将存储库类(实现DAO)的实例注入另一个类时,我发现了一些问题 因此,我将我的存储库类命名为KMProjectInfo-ServiceImpl: @Repository("kmProjectInfoService") public class KMProjectInfoServiceImpl extends AbstractService implements KMProjectInfoService { public List<KM_Project

使用@Autowired注释将存储库类(实现DAO)的实例注入另一个类时,我发现了一些问题

因此,我将我的存储库类命名为KMProjectInfo-ServiceImpl

@Repository("kmProjectInfoService")
public class KMProjectInfoServiceImpl extends AbstractService implements KMProjectInfoService {


    public List<KM_ProjectInfo> getProjectInfoList() {
        return getHibernateTemplate().execute(
                new HibernateCallback<List<KM_ProjectInfo>>() {
                    public List<KM_ProjectInfo> doInHibernate(Session session) throws HibernateException, SQLException {
                        return getProjectsInformationsList(session);
                    }
                }
        );
    }

    public List<KM_ProjectInfo> getProjectInfoListByFolderTech() {
        return getHibernateTemplate().execute(
                new HibernateCallback<List<KM_ProjectInfo>>() {
                    public List<KM_ProjectInfo> doInHibernate(Session session) throws HibernateException, SQLException {
                        return getProjectsInformationsListByFolderTech(session);
                    }
                }
        );
    }

    public KM_ProjectInfo getProjectInfobyId(long idProjectInfo) {
        final Long id = idProjectInfo;
        return getHibernateTemplate().execute(
                new HibernateCallback<KM_ProjectInfo>() {
                    public KM_ProjectInfo doInHibernate(Session session) throws HibernateException, SQLException {
                        return getProjectInfo(id, session);
                    }
                }
        );
    }

    public void save(KM_ProjectInfo projectInfo) {
        // L'oggetto non è già presente sulla tabella
        if(projectInfo.getIdProjectInfo() == null){
            getHibernateTemplate().persist(projectInfo);    // lo rende persistente
        }
        // L'oggetto è già presente sulla tabella
        else {
            getHibernateTemplate().merge(projectInfo);      // fa l'update
        }
    }


    private List<KM_ProjectInfo> getProjectsInformationsList(Session session) {
        // Create query:
        Query query = session.getNamedQuery("kmProjectInfoList");
        List<KM_ProjectInfo> projectInfoList =  query.list();
        return projectInfoList;
    }

    private List<KM_ProjectInfo> getProjectsInformationsListByFolderTech(Session session) {
        // Create query:
        Query query = session.getNamedQuery("kmProjectInfoByFolderTech");
        List<KM_ProjectInfo> projectInfoList =  query.list();
        return projectInfoList;
    }

    private KM_ProjectInfo getProjectInfo(Long id, Session session) {
        // Create query:
        Query query = session.getNamedQuery("kmProjectInfoById");
        query.setParameter("projectInfoId", id);
        KM_ProjectInfo projectInfo = (KM_ProjectInfo) query.uniqueResult();
        return projectInfo;
    }
}
因此,现在我必须将此存储库注入名为ConfigurationProjectAction的控制器类,该类必须将其用作依赖项

所以我有这样的想法:

@Controller("confProjectActionController")
@Scope("prototype")
public class ConfigurationProjectAction extends KMAction implements PortletPreferencesAware {

    @Autowired
    @Qualifier("kmProjectInfoService")
    private KMProjectInfoServiceImpl

    ................................................
    ................................................
    ................................................
    ................................................
}
正如您在前面的代码片段中所看到的,我试图使用@Autowired注释来注入具有指定名称为kmProjectInfoService的存储库(使用@Qualifier注释)

我的IDE没有标记错误,也没有编译时错误,但当我尝试执行应用程序时,我在stacktrace中获得以下错误消息:

2015-01-08 10:07:29,321 [[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR commons.CommonsLogger.error(38) - Could not execute action
Unable to instantiate Action, confProjectActionController,  defined for 'startConfigurationProjectAction' in namespace '/configProject'Error creating bean with name 'confProjectActionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private egp.prc.km.services.KMProjectInfoServiceImpl egp.prc.km.actions.configurationProject.ConfigurationProjectAction.kmProjectInfoServiceRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [egp.prc.km.services.KMProjectInfoServiceImpl] 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=kmProjectInfoService)}
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:318)
    at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:399)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:198)

    ..........................................................................
    ..........................................................................
    ..........................................................................
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private egp.prc.km.services.KMProjectInfoServiceImpl egp.prc.km.actions.configurationProject.ConfigurationProjectAction.kmProjectInfoServiceRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [egp.prc.km.services.KMProjectInfoServiceImpl] 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=kmProjectInfoService)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)

    ..........................................................................
    ..........................................................................
    ..........................................................................

为什么??这一错误的原因可能是什么?我错过了什么?如何尝试修复它并将存储库注入控制器类?

尝试不注入接口KMProjectInfo服务的实现,而是直接注入接口。我还建议删除注释级别上的存储库名称:

@Repository(“KMProjectInfo服务”)

使用
@Repository

在此之后,您的接口可以用于注入,因为它不会和存储库实现的名称重叠


Spring使用代理模式进行注入。因此,您需要插入接口。

@autowiredwiresbytype默认值。所以,若在spring容器中只有一个匹配的bean,那个么就不需要使用@Qualifier。试试下面

@Autowired
//@Qualifier("kmProjectInfoService")
private KMProjectInfoService kmProjectInfoService;

stacktrace中没有更多内容了?您需要编程到接口,而不是具体的类。将
KMProjectInfo服务impl
更改为
KMProjectInfo服务
@M.Deinum ok,并让我使用限定符注释来指定要使用的具体实现是什么?否,除非您有同一接口的多个IMLPement。请提供您的应用程序上下文。xmlSpring是注入的人,而不是开发人员。Spring使用代理模式进行注入是什么意思?我不知道代理与注入有什么关系。@zeroflagL Spring框架使拦截对给定bean的方法调用成为可能,前提是bean实现了一些接口。Spring框架将bean包装在动态代理中。所有对bean的调用都会被代理截获。代理可以决定在将方法调用委托给封装的bean之前,而不是之后,在其他对象上调用其他方法。还值得注意的是,可以在没有接口的情况下创建代理,并且仅在必要时创建代理。Spring不会为每个bean创建代理。
@Autowired
//@Qualifier("kmProjectInfoService")
private KMProjectInfoService kmProjectInfoService;