gpt4 book ai didi

Java spring noob bean 和配置

转载 作者:行者123 更新时间:2023-12-01 12:47:48 25 4
gpt4 key购买 nike

我正在尝试运行休耕类。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com