[SpringBoot]:简单组件无法自动装配String类

问题描述

我有这个简单的组件类:

package jason;

import org.springframework.stereotype.Component;

@Component
public class Messenger {
    private String message;


    public Messenger(String message) {
        this.message = message;
    }

    public void setMessage(String message){
        this.message  = message;
    }

    public void getMessage(){
        System.out.println("Your Message : " + message);
    }
}

在构造函数的参数中,IntelliJ报告:Could not autowire. No beans of 'String' type found.

这个小型玩具项目还有另外两个类:Config

package jason;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackageClasses = Messenger.class)
public class Config {
    @Bean
    public Messenger helloWorld(){
        return new Messenger("Hello World!");
    }
}

MainApp

package jason;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        Messenger obj = context.getBean("helloWorld",Messenger.class);
        obj.getMessage();
    }
}

奇怪的是,除了看似编译时的错误,该项目还在构建,但在运行时失败,原因是:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'messenger' defined in file [C:\Users\jasonfil\code\green-taxi\target\classes\jason\Messenger.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: {}

在这里做错了什么? SpringBoot的新手。可能会有IOC的误解。

解决方法

您在Messenger类中混合了两种注入方式,在@Component中基于注解进行了注入方式,并且在配置类中使用@Bean将其声明为bean。

当尝试通过激活组件扫描的Messenger注入AnnotationConfigApplicationContext时,Spring将首先使用基于注释的注入,因此@Component

因此,如果没有基于构造函数的自动装配(就是您的情况),那么Spring将调用默认构造函数来注入您的bean,因此您需要将默认构造函数添加到Messenger中。如果没有默认的构造函数,Spring将使用可用的构造函数,因此将得到上面的错误。当然,您需要删除@Bean配置,因为您没有使用它: 包jason;

import org.springframework.stereotype.Component;

@Component
public class Messenger {

    private String message;

    public Messenger() {
    }

    public Messenger(String message) {
        this.message = message;
    }

    public void setMessage(String message){
        this.message  = message;
    }

    public void getMessage(){
        System.out.println("Your Message : " + message);
    }
}

或者,如果要使用Bean配置,则可以从@Component中删除Messenger,也可以删除@ComponentScan

package jason;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public Messenger helloWorld(){
        return new Messenger("Hello World!");
    }
}
,

您正在使用Java Config类型Bean注册以及组件扫描类型Bean注册。

最快的解决方案是从ports: - containerPort: 8080 protocol: TCP 类中删除@Component