gpt4 book ai didi

java - Bean创建异常: Injection of autowired dependencies failed

转载 作者:太空宇宙 更新时间:2023-11-04 07:05:23 25 4
gpt4 key购买 nike

我在 2 个不同的项目中有 2 个类,并且在 Autowiring 字段时遇到一些困难。

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

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");
}
}

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

package com.bbb

public interface CurveAccess {
// some methods
}

由 CurveDAO 类实现:

package com.bbb.impl

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

我的 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>

运行时,出现以下异常:

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)}

如何解决这个问题?

最佳答案

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

@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();
}

}

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

关于java - Bean创建异常: Injection of autowired dependencies failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21481801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com