java - BeanInstantiationException : Failed to instantiate : No default constructor found;

标签 java spring spring-boot

我正在试验 Spring 5,当我运行测试应用程序时,它会抛出此异常。我知道,如果我们对字段和 Setter 方法使用 @Autowired,则需要一个零参数构造函数,因为首先使用零参数构造函数实例化 bean,然后注入(inject)依赖 bean。但是,如果构造函数是 @Autowired,为什么我们需要一个零参数构造函数,因为该构造函数将用于实例化 bean?

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)

Student.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;
    }
    
}

PersonalInfo.java

@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...";
    }
}

测试.java

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>

编辑

更新测试类后,我得到

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'student' available.

最佳答案

您的问题是您将注释配置与 XML 配置混合在一起。 由于您创建了 ClassPathXmlApplicationContext 的实例,因此只有 XML 中的内容才会被视为配置,并且您的 @Autowired 注释将被忽略。

要修复 xml 的错误,您需要在 xml 定义中添加 bean 注入(inject):

    <bean id="student" class="com.spring.hello.Student">
        <constructor-arg ref="personalInfo" />
    </bean>
    <bean id="personalInfo" class="com.spring.hello.personalInfo"></bean>

如果您想使用注释配置,则可以创建 AnnotationConfigApplicationContext 的实例。然后,您仍然需要使用@Component注释student和personalInfo,以便Spring在扫描提供的包时可以将它们作为bean找到。

@Component
public class PersonalInfo {
  ...
}

@Component
public class Student{
   ...
}
public static void main(String[] args) {
    var ctx = new AnnotationConfigApplicationContext();
    ctx.scan("com.spring.hello");
    ctx.refresh();
}

关于java - BeanInstantiationException : Failed to instantiate : No default constructor found;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66545142/

相关文章:

java - 有没有办法消除 Java 中早期方法返回的冗余检查?

java - 解决Java Oracle并发更新/删除语句

java - apache shiro : authc. loginUrl 不起作用

java - 使用标准输入通过/proc/{pid}/fd/0 向 java -jar 发送命令

spring - 错误 : cannot execute nextval() in a read-only transaction - PostgreSQL

java - Spring Boot + Mysql中如何实现数据的版本控制?

spring-boot - 是否可以将 Spring Boot 应用程序部署到 App Engine 并连接到数据库?

java - Log4J dailyrolling fileappender,控制滚动文件名

spring - 将 Spring MBean 导出到 Tomcat 的 MBeanServer 实例

java - activiti 6 中表达中使用的未知方法