Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSpringNoobbean和配置_Java_Spring_Autowired_Spring Bean - Fatal编程技术网

JavaSpringNoobbean和配置

JavaSpringNoobbean和配置,java,spring,autowired,spring-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.Sp

我在试着上休闲课

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

}
第二级考试:

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注释掉公共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)}

为什么在第一种情况下会找到Test2bean,而当我从AppConfig中注释掉它时,它就再也找不到它了(在第一条错误消息中,您可以清楚地看到找到了test2)?@Configure annotation是否必须将bean添加到上下文中?

Autowired的工作原理是否首先尝试按类型查找所需的bean,而回退机制是按bean名称(例如:同一类型有多个bean)

但是当您从
AppConfig
禁用
@Bean
注释时,应该至少有一个类型为Test2的Bean,正如您用
@Component
注释
Test2
一样,但它找不到它。要解决这个问题,您需要在
AppConfig
类上添加
@ComponentScan(basePackageClasses={Test2.class})
,如下所示

@Configuration
@ComponentScan(basePackageClasses = { Test2.class })
public class AppConfig {
@Bean
public Test gettest(){
    return new Test();
}

//@Bean
public Test2 gettest2(){
    return new Test2();
}
}
@Configuration
@ComponentScan(basePackageClasses = { Test2.class })
public class AppConfig {
@Bean
public Test gettest(){
    return new Test();
}

@Bean
public Test2 gettest2(){
    return new Test2();
}
}
但是如果您按如下方式启用bean注释

@Configuration
@ComponentScan(basePackageClasses = { Test2.class })
public class AppConfig {
@Bean
public Test gettest(){
    return new Test();
}

//@Bean
public Test2 gettest2(){
    return new Test2();
}
}
@Configuration
@ComponentScan(basePackageClasses = { Test2.class })
public class AppConfig {
@Bean
public Test gettest(){
    return new Test();
}

@Bean
public Test2 gettest2(){
    return new Test2();
}
}
您需要在测试时使用更改
Test2
的注入

 @Autowired
 Test2 test2;
或者使用

 @Autowired
 Test2 gettest2;

当您定义
@Component
时,您定义了一个可用于自动布线的bean

当您定义
@Bean
(在`@配置中)时,您定义了一个可用于自动布线的Bean

因此,您定义了两个相同的bean,这导致spring无法知道哪一个是要自动连接的正确bean


解决方案是只定义一次

在Spring/Spring引导中,有几种方法可以使类成为一个
bean

  • 使用
    @Component
    或任何其他派生注释(
    @Service
    @Repository
    @Controller
    @RestController
    )注释类
  • 在XML配置中将类定义为
    bean
  • 使用
    @Bean
    注释(此注释只能应用于方法指定它返回一个由Spring上下文管理的Bean)<代码>@Bean注释通常在配置类方法中声明
当Spring初始化其上下文时,它会查找任何bean定义并创建bean。在您的情况下,它会发现
test
test2
的重复声明,因此您需要从类中删除配置类或
@组件
注释


有关更多信息,请参见

是,这解决了问题,但这不是我不理解的。当谈到春天如何管理豆子时,我错过了更多的大局。文章末尾的问题是,如果我从中注释掉public test2 gettest2(),为什么找不到test2 bean了AppConfig@user1934513它现在回答你的问题了吗?是的。使用ComponentScan是有意义的。谢谢。如果我删除配置类,我需要在main()函数中使用什么bean工厂,而不是AnnotationConfigApplicationContext(AppConfig.class)?是否存在收集包中所有bean的默认组件?从类中删除@Component注释是有效的。AnnotationConfigApplicationContext(test.class,test2.class)哦,我没有注意到您正在围绕它创建应用程序上下文。您可以注入上下文(这真的不是一个好主意),也可以只是去掉bean,但保留配置