Java BeanInstationException:实例化失败:未找到默认构造函数;
我正在试用Spring5,当我运行测试应用程序时,它抛出了这个异常。我理解,如果我们对字段和Setter方法使用@Autowired,我们需要一个零参数构造函数,因为bean首先用零参数构造函数实例化,然后注入依赖bean。但是,如果构造函数是@Autowired的,那么为什么我们需要一个零参数构造函数,因为这个构造函数将用于实例化beanJava BeanInstationException:实例化失败:未找到默认构造函数;,java,spring,spring-boot,Java,Spring,Spring Boot,我正在试用Spring5,当我运行测试应用程序时,它抛出了这个异常。我理解,如果我们对字段和Setter方法使用@Autowired,我们需要一个零参数构造函数,因为bean首先用零参数构造函数实例化,然后注入依赖bean。但是,如果构造函数是@Autowired的,那么为什么我们需要一个零参数构造函数,因为这个构造函数将用于实例化bean is org.springframework.beans.BeanInstantiationException: Failed to instantiate
is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.spring.hello.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.spring.hello.Student.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
PersonalInfo.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Student {
private PersonalInfo pI;
@Autowired
public Student (PersonalInfo pI) {
this.pI = pI;
}
}
@Component
public class PersonalInfo {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private PersonalInfo() {
System.out.println("constructor...");
}
public String printHello() {
return "PersonalInfo...";
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.hello.Student;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext("/com/spring/resources/applicationContext.xml");
Student bean = (Student) context.getBean("student");
System.out.println(bean);
}
}
Test.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Student {
private PersonalInfo pI;
@Autowired
public Student (PersonalInfo pI) {
this.pI = pI;
}
}
@Component
public class PersonalInfo {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private PersonalInfo() {
System.out.println("constructor...");
}
public String printHello() {
return "PersonalInfo...";
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.hello.Student;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext("/com/spring/resources/applicationContext.xml");
Student bean = (Student) context.getBean("student");
System.out.println(bean);
}
}
ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:annotation-config/>
<bean id="student" class="com.spring.hello.Student">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="personalInfo" class="com.spring.hello.personalInfo">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>
您的问题是将注释配置与XML配置混合在一起。 因为您创建了
ClassPathXmlApplicationContext
的实例,所以只有XML中的内容将被视为配置,并且您的@Autowired
注释将被忽略
要修复xml错误,需要在xml定义中添加bean注入:
如果要改用注释配置,可以创建AnnotationConfigApplicationContext
的实例。然后,您仍然需要使用@Component
注释student和personalInfo,以便Spring在扫描提供的包时可以将它们作为bean找到
@组件
公共类个人信息{
...
}
@组成部分
公立班学生{
...
}
publicstaticvoidmain(字符串[]args){
var ctx=新的AnnotationConfigApplicationContext();
ctx.scan(“com.spring.hello”);
ctx.refresh();
}
您可以共享您的应用程序上下文.xml
?作为旁注;您的域对象不应是beans.Approved。这只是为了理解:)更新我已经更新了。但这一次,我得到了“线程中的异常”main“org.springframework.beans.factory.nosuchbeanddefinitionexception:没有名为“student”的bean可用”异常。@skgvnew AnnotationConfigApplicationContext(“/com/spring/resources/applicationContext.xml”)
是错误的。只需从我的帖子中复制/粘贴初始化。