Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Java 我们需要在SpringBoot中为所有内置类创建bean吗?_Java_Spring_Spring Boot_Rabbitmq_Spring Rabbit - Fatal编程技术网

Java 我们需要在SpringBoot中为所有内置类创建bean吗?

Java 我们需要在SpringBoot中为所有内置类创建bean吗?,java,spring,spring-boot,rabbitmq,spring-rabbit,Java,Spring,Spring Boot,Rabbitmq,Spring Rabbit,我正在尝试使用Spring引导配置RabbitMQ。下面是我的配置类的快照 案例1: @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setMessageConverter(new Jack

我正在尝试使用Spring引导配置RabbitMQ。下面是我的配置类的快照

案例1:

   @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());            
        return template;
    }
这段代码运行良好

代码2:

       @Bean
        public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
            RabbitTemplate template = new RabbitTemplate(connectionFactory);
    //      template.setMessageConverter(new Jackson2JsonMessageConverter()); // Line 1 - works
   //       template.setMessageConverter(messageConverter); // Line 2 - error: asks to inject Bean
            return template;
        }
然而,在本例中,如果我使用第1行,代码就可以正常工作,在那里我正在创建Jackson2JsonMessageConverter的对象

但我写这段代码是为了理解已经存在的代码的工作原理,其中使用了第2行而不是第1行。因此,当我使用第2行而不是第1行时,我得到错误:

考虑在配置中定义“org.springframework.amqp.support.converter.MessageConverter”类型的bean

所以我有两个问题:

  • 为什么会出现这种错误
  • 如果我为MessageConverter定义一个bean,比如

    @豆子 public MessageConverter createMessageConverter(){ 返回新的Jackson2JsonMessageConverter(); }

  • 然后它就起作用了。那为什么不要求我为ConnectionFactory参数定义一个bean呢


    PS:这里和我试图理解的代码中都没有使用@Autowired,这两个参数、ConnectionFactory和MessageConverter都是接口而不是类。简单的回答是:您需要理解Spring Boot中自动配置的概念,这将为您创建大量的@Bean,而不需要“看到它们”

    了解自动配置的一篇非常好的文章是:


    在您的例子中,您可能还想看看Spring Boot源代码中的“RabbitAutoConfiguration”类。

    简单的回答是:您需要理解Spring Boot中自动配置的概念,它将为您创建大量@Bean,而您不会“看到它们”

    了解自动配置的一篇非常好的文章是:


    在您的例子中,您可能还想看看Spring Boot源代码中的“RabbitAutoConfiguration”类。

    嘿,伙计。。。我是YouTube的订户。。。很棒的视频。。。这一概念在你的Spring Boot视频中有更详细的解释吗?谢谢:)我没有把我所有的Spring Boot视频都放在YouTube上,有人说我在解释它,但它是用德语写的。尝试一下这篇文章,它试图用非常简单的方式解释它,而且是最新的。希望有帮助!我读了这篇文章。消除了我的疑虑。事实上,有一个父spring引导项目,其配置包含MessageConverter Bean的定义。此项目添加到pom中,其配置包含在使用@Import的当前项目配置中。非常感谢。嘿,伙计。。。我是YouTube的订户。。。很棒的视频。。。这一概念在你的Spring Boot视频中有更详细的解释吗?谢谢:)我没有把我所有的Spring Boot视频都放在YouTube上,有人说我在解释它,但它是用德语写的。尝试一下这篇文章,它试图用非常简单的方式解释它,而且是最新的。希望有帮助!我读了这篇文章。消除了我的疑虑。事实上,有一个父spring引导项目,其配置包含MessageConverter Bean的定义。此项目添加到pom中,其配置包含在使用@Import的当前项目配置中。谢谢。