Java BeanCreationException : 自动装配依赖项的注入失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21481801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:56:16  来源:igfitidea点击:

BeanCreationException : Injection of autowired dependencies failed

javaspring-bean

提问by l0r3nz4cc10

I have 2 classes in 2 different projects, and I have some difficulties to autowire a field.

我在 2 个不同的项目中有 2 个课程,并且在自动装配一个字段时遇到了一些困难。

In project pack, I have this Computation class :

在项目包中,我有这个计算类:

package fr.aaa;

@Component
public class Computation {
    @Autowired
    @Qualifier("curveDAO")
    CurveAccess curveDAO;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
    }
}

In project db, I have this CurveAccess interface :

在项目数据库中,我有这个 CurveAccess 接口:

package com.bbb

public interface CurveAccess {
    // some methods
}

implemented by a CurveDAO class :

由 CurveDAO 类实现:

package com.bbb.impl

@Repository("curveDAO")
@Transactional("cvaTxManager")
public class CurveDAO implements CurveAccess {
    // some methods
}

My applicationContext.xml file from pack project :

我来自包项目的 applicationContext.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:security="http://www.springframework.org/schema/security" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd      
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">  

<import resource="classpath:spring/persistence.xml"/>

<context:annotation-config />
<context:component-scan base-package="fr.aaa.*, com.bbb.*"/> 

<util:properties id="jdbcProps" location="jdbc.properties" />   

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
         <value>classpath:configuration.properties</value>
         <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

When running, I have this exception :

运行时,我有这个例外:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Computation': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.bbb.CurveAccess fr.aaa.Computation.curveDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bbb.CurveAccess] 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=curveDAO)}

How can I solve this problem ?

我怎么解决这个问题 ?

回答by Mustafa Gen?

You need a class that implements CurveAccessinterface

你需要一个实现CurveAccess接口的类

@Component
public class CurveAccessImpl implements CurveAccess {
    //methods
}

Also remove Qualifierfrom curveDaoin Computationbean since there is no such bean with id curveDao.

同时删除QualifiercurveDaoComputation因为不存在与ID没有这样的豆豆curveDao

@Autowired
CurveAccess curveDAO;

回答by Dherik

Actually, your code works perfectly fine:

实际上,您的代码运行良好:

@SpringBootApplication
public class So21481801Application implements CommandLineRunner {

    public interface CurveAccess {
        String hello();
    }

    @Repository("curveDAO")
    public class CurveDAO implements CurveAccess {
        @Override
        public String hello() {
            return "Hello";
        }
    }

    @Repository("curveDAOWorld")
    public class CurveDAOWorld implements CurveAccess {
        @Override
        public String hello() {
            return "World";
        }
    }

    @Autowired
    @Qualifier("curveDAO")
    CurveAccess curveDAO;

    @Autowired
    @Qualifier("curveDAOWorld")
    CurveAccess curveDAOWorld;

    @Override
    public void run(String... args) {
        System.out.println(curveDAO.hello());
        System.out.println(curveDAOWorld.hello());
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(So21481801Application.class, args);
        context.close();
    }

}

Probably you have some problem on how you are using the component-scanconfiguration on XML and the Java annotations. Mix them is not a good idea.

可能您对如何使用component-scanXML 和 Java 注释上的配置有一些问题。混合它们不是一个好主意。