【问题标题】:BeanInstantiationException: Failed to instantiate : No default constructor found;BeanInstantiationException:无法实例化:未找到默认构造函数;
【发布时间】:2021-06-07 05:43:52
【问题描述】:

我正在尝试使用 Spring 5,当我运行测试应用程序时,它会抛出此异常。我知道如果我们将 @Autowired 用于 Fields 和 Setter 方法,我们需要一个零参数构造函数,因为首先使用零参数构造函数实例化 bean,然后注入依赖 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...";
    }
}

Test.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.

【问题讨论】:

  • 你能分享你的applicationContext.xml吗?作为旁注;你的域对象不应该是 bean。
  • 同意。这只是为了理解:) 更新

标签: java spring spring-boot


【解决方案1】:

您的问题是您将注释配置与 XML 配置混合在一起。 因为您创建了 ClassPathXmlApplicationContext 的实例,所以只有 XML 中的内容将被视为配置,而您的 @Autowired 注释将被忽略。

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

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

【讨论】:

  • 我已经更新了。但是这一次,我收到“线程“主”org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“学生”的bean 中的异常”异常。
  • @skgv new AnnotationConfigApplicationContext("/com/spring/resources/applicationContext.xml") 是错误的。只需复制/粘贴我帖子中的初始化即可。
猜你喜欢
  • 2016-10-29
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多