有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java[SpringBoot]:简单组件无法自动连接字符串类

我有一个简单的组件类:

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!");
    }
}

和{}:

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的新功能。可能有国际奥委会的误解


共 (2) 个答案

  1. # 1 楼答案

    您将两种bean注入方式与Messenger类混合,将基于注释的注入方式与@Component混合,并在配置类中将其声明为一个bean,与@Bean

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

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

    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配置,可以从Messenger中删除@Component,也可以删除@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!");
        }
    }
    
  2. # 2 楼答案

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

    最快的解决方案是从Messenger类中删除@Component