gpt4 book ai didi

java - 即使使用所有注释和组件扫描后也会出现 NoSuchBeanDefinitionException

转载 作者:行者123 更新时间:2023-12-02 11:27:48 27 4
gpt4 key购买 nike

我正在获取

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.learn.stackoverflow.general.Person com.learn.stackoverflow.general.SpringTest.person; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.learn.stackoverflow.general.Person] 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)}

即使在使用了所有注释之后,我已经用 @Component 注释了所有类并添加了组件扫描,但 @Autowired 仍然给我错误。

以下是我的类(class):

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;

@Component
public class SpringTest {

@Autowired
Person person;

public static void main(String[] args) {
testApplicationContext();
}

private static void testApplicationContext() {
// here static and instance initializers of Person class will be invoked right away, even when we are not calling getBean method
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\E_Drive\\Projects\\Workspace\\Test\\CS101\\src\\com\\learn\\stackoverflow\\general\\bean.xml");
SpringTest springTest = (SpringTest) applicationContext.getBean("springTest");
}

}

人员类别:

import org.springframework.context.annotation.Scope;
import com.bea.core.repackaged.springframework.stereotype.Component;

@Component
@Scope(value="singleton")
public class Person {
static{
System.out.println("1");
}

{
System.out.println("2");
}
}

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.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.learn.stackoverflow.general"/>

<!-- <bean id = "person" class = "com.learn.stackoverflow.general.Person" scope="singleton">
</bean> -->

<bean id = "springTest" class = "com.learn.stackoverflow.general.SpringTest" scope="singleton">
</bean>

</beans>

最佳答案

Person.class中使用正确的导入。而不是

import com.bea.core.repackaged.springframework.stereotype.Component;

你必须使用

import org.springframework.stereotype.Component;

顺便说一句,你可以避免使用

@Scope(value="singleton")

因为 singleton 是 Spring beans 的默认作用域。

关于java - 即使使用所有注释和组件扫描后也会出现 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49491093/

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