Java 即使在使用所有注释和组件扫描之后,也无法获取NoSuchBeanDefinitionException
我越来越 org.springframework.beans.factory.BeanCreationException:错误 创建名为“springTest”的bean:自动连线的注入 依赖关系失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法 autowire字段:com.learn.stackoverflow.general.Person com.learn.stackoverflow.general.SpringTest.person;嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 找到[com.learn.stackoverflow.general.Person]类型的合格bean 对于依赖项:至少需要1个符合autowire条件的bean 此依赖项的候选项。依赖项批注: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 即使在使用了所有注释之后,我已经用Java 即使在使用所有注释和组件扫描之后,也无法获取NoSuchBeanDefinitionException,java,spring,Java,Spring,我越来越 org.springframework.beans.factory.BeanCreationException:错误 创建名为“springTest”的bean:自动连线的注入 依赖关系失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法 autowire字段:com.learn.stackoverflow.general.Person com.learn.stackoverflow.general.Sp
@Component
注释了我的所有类,并添加了组件扫描,但@Autowired
仍然给我带来了错误
以下是我的课程:
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
是Springbeans的默认作用域。如果您没有使用XML文件创建Bean,那么我认为您需要使用@Bean
对它们进行注释,谢谢,只有一个问题-假设我正在使用@Component
注释类,那么我仍然需要在我的Springbean配置文件中定义bean。不,您不需要。只需确保组件扫描包含正确的软件包。