Java spring noob bean 和配置

标签 java spring autowired spring-bean

我正在尝试运行休耕类。

package com.example.demo;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        var factory = new AnnotationConfigApplicationContext(AppConfig.class);
        test tst = factory.getBean(test.class);
        tst.getDummy().test();
        SpringApplication.run(DemoApplication.class, args);
    }

}
应用配置类:
package com.example.demo;

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

@Configuration
public class AppConfig {
    @Bean
    public test gettest(){
        return new test();
    }
//
    @Bean
    public test2 gettest2(){
        return new test2();
    }
}
第一个类:
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
public class test{
    @Autowired
    test2 dummy;

    public void setDummy(test2 dummy) {
        this.dummy = dummy;
    }

    public test2 getDummy() {
        return dummy;
    }

    void test(){
        System.out.println("test");
    }
}
第二个测试类:
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class test2{

    public test2(){

    }
    void test(){
        System.out.println("test2");
    }

}
我在运行时收到此错误
Field dummy in com.example.demo.test required a single bean, but 2 were found:
    - test2: defined in file [/Users/biliuta/IdeaProjects/demo/build/classes/java/main/com/example/demo/test2.class]
    - gettest2: defined by method 'gettest2' in class path resource [com/example/demo/AppConfig.class]
如果我从 AppConfig 注释掉 public test2 gettest2() bean,我会得到这个错误:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gettest': Unsatisfied dependency expressed through field 'dummy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.test2' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
为什么在第一种情况下找到了 test2 bean,当我从 AppConfig 中注释掉它时,它再也找不到了(在第一条错误消息中,您可以清楚地看到找到了 test2)? @Configure 注释是否必须将 bean 添加到上下文?

最佳答案

当您定义 @Component您定义了一个可用于 Autowiring 的 bean
当您定义 @Bean (在`@Configuration 中)你定义了一个可以 Autowiring 的bean
所以你定义了两次相同的 bean,这导致 spring 无法知道哪个是正确的 bean 来 Autowiring
解决方案是只定义一次

关于Java spring noob bean 和配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62552603/

相关文章:

java - 无法自动启动 bean tomcat IntelliJ

java - 如果扫描器接收到除 int 之外的其他内容,则会创建无限循环

java - 在 Java 中将枚举变量递增到下一个元素

spring - 为所有实体编写通用 spring jpa 存储库

java - 如何在 Spring-Cloud-Stream 中配置重新连接到 Kafka

spring - Karaf 上的 Spring 应用程序示例

java - Java 中的正则表达式——只返回最后一个匹配项

java - "deWiTTERS Game Loop"是否假设不间断电源?

spring - 为什么这个 Spring 应用程序中的过滤器中的 Autowiring bean 为空?

java - NoUniqueBeanDefinitionException : . .. 预期单个匹配 bean 但找到 2: