使用参数在构造函数上自动装配失败?

Autowired on Constructor with parameters fails?

我有以下示例代码:

豆子class:

package com.example.learn1.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Bean1 {

    private String name;
    private int id;

    public Bean1() {
        System.out.println("Inside Bean1() constructor");
    }

    @Autowired
    public Bean1(String name, int id) {
        System.out.println("Inside Bean1(String name, int id) constructor");
        this.name = name;
        this.id = id;
    }

    public Bean1(String name) {
        System.out.println("Inside Bean1(String name) constructor");
        this.name = name;
    }

    public Bean1(int id) {
        System.out.println("Inside Bean1(int id) constructor");
        this.id = id;
    }

}

下面是主要的应用程序,我在这里尝试自动装配 bean1。

package com.example.learn1.main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.example.learn1.bean.Bean1;
@Configuration
@ComponentScan(basePackages = { "com.example.learn1" })
@Component
public class MainApp {

    @Autowired
    Bean1 bean1;

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(MainApp.class);
        System.out.println("Inside main, ctx is --> " + ctx);
        MainApp t = ctx.getBean(MainApp.class);
        t.print();
    }

    public void print() {
        System.out.println("Inside MainApp#print() method");
    }
}

当我不在构造函数 public Bean1(String name, int id) 上保留 @Autowire 时,程序运行良好。默认情况下,它调用无参数构造函数即:public Bean1()。当我在构造函数 public Bean1(String name, int id) 上使用 @Autowire 时,它失败了。

那么它只适用于无参数构造函数吗?我怎样才能使它适用于带参数的构造函数?

谁能帮我理解这种行为?

以下是例外情况:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainApp': Unsatisfied dependency expressed through field 'bean1'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bean1' defined in file [/Users/vkoul/eclipse-workspace/spring-framework-learning/target/classes/com/example/learn1/bean/Bean1.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=12=](AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
    at com.example.learn1.main.MainApp.main(MainApp.java:37)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bean1' defined in file [/Users/vkoul/eclipse-workspace/spring-framework-learning/target/classes/com/example/learn1/bean/Bean1.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=12=](AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1244)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 14 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1651)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    ... 27 more
@Autowired
public Bean1(String name, int id) {
    System.out.println("Inside Bean1(String name, int id) constructor");
    this.name = name;
    this.id = id;
}

告诉容器在构造函数时注入 2 个 bean(一个 String 类型的 bean 称为 name,另一个称为 id 的类型 int)用于实例化bean1。 你在哪里定义那些bean?另请注意,从基本类型创建 bean 并不常见,但这并不意味着您不能。

对于原始类型,我们通常在application.properties中声明它们,然后使用@Value注入它们。