自动装配和重用 "Spring-defined" 属性
Autowire and reuse "Spring-defined" properties
我有一个简单的问题,但我在文档或任何示例中都找不到相关信息。
我有一个 Spring 引导项目,它使用 RabbitMQ 和 Spring 引导 AMQP 模块。
我正在使用特定的交换和 Spring Jackson2JsonMessageConverter
所以我需要定义我自己的 RabbitTemplate
bean,我想(在这一点上,如果有人知道是否可以做另一个更简单的方法,不客气。
@Bean
RabbitTemplate myAwesomeTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.setExchange(myBeautifulExchange);
rabbitTemplate.setRoutingKey("legendary");
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.setConnectionFactory(connectionFactory());
return rabbitTemplate;
}
这里对我来说真正的问题是我也必须定义一个 CachingConnectionFactory
bean,只是为了将它设置为模板,但似乎 CachingConnectionFactory
已经由 Spring RabbitAutoConfiguration
class,所以我想知道我是否可以在我的 class:
中简单地这样做
@Autowired
CachingConnectionFactory cachingConnectionFactory
但是 Intellij 一直在抱怨,因为它找不到那个 bean 的定义,就好像它是某种定义顺序问题之类的......
当我以这种方式启动项目时,它看起来还不错,但我不喜欢 Intellij 抱怨,就像我在做某事 "not the standard way".
提前谢谢大家!抱歉解释太长了。
PS:如果我只想自动装配 Spring RabbitProperties
bean 并使用主机、用户名和密码属性(分别为 spring.rabbitmq.host
、spring.rabbitmq.username
和 spring.rabbitmq.password
).
But Intellij keeps complaining because it cannot find the definition ...
I don't like when Intellij complains, it's like I'm doing something "not the standard way".
你可以尝试通过将它添加到工厂方法并使用参数注入来欺骗它...
@Bean
RabbitTemplate myAwesomeTemplate(ConnectionFactory rabbitConnectionFactory) {
...
}
与属性相同。
无论哪种方式(@Autowired
或这一种)都是 "the standard way"。
如果它仍然报错,我建议您与 JetBrains 联系。
我有一个简单的问题,但我在文档或任何示例中都找不到相关信息。
我有一个 Spring 引导项目,它使用 RabbitMQ 和 Spring 引导 AMQP 模块。
我正在使用特定的交换和 Spring Jackson2JsonMessageConverter
所以我需要定义我自己的 RabbitTemplate
bean,我想(在这一点上,如果有人知道是否可以做另一个更简单的方法,不客气。
@Bean
RabbitTemplate myAwesomeTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.setExchange(myBeautifulExchange);
rabbitTemplate.setRoutingKey("legendary");
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.setConnectionFactory(connectionFactory());
return rabbitTemplate;
}
这里对我来说真正的问题是我也必须定义一个 CachingConnectionFactory
bean,只是为了将它设置为模板,但似乎 CachingConnectionFactory
已经由 Spring RabbitAutoConfiguration
class,所以我想知道我是否可以在我的 class:
@Autowired
CachingConnectionFactory cachingConnectionFactory
但是 Intellij 一直在抱怨,因为它找不到那个 bean 的定义,就好像它是某种定义顺序问题之类的...... 当我以这种方式启动项目时,它看起来还不错,但我不喜欢 Intellij 抱怨,就像我在做某事 "not the standard way".
提前谢谢大家!抱歉解释太长了。
PS:如果我只想自动装配 Spring RabbitProperties
bean 并使用主机、用户名和密码属性(分别为 spring.rabbitmq.host
、spring.rabbitmq.username
和 spring.rabbitmq.password
).
But Intellij keeps complaining because it cannot find the definition ... I don't like when Intellij complains, it's like I'm doing something "not the standard way".
你可以尝试通过将它添加到工厂方法并使用参数注入来欺骗它...
@Bean
RabbitTemplate myAwesomeTemplate(ConnectionFactory rabbitConnectionFactory) {
...
}
与属性相同。
无论哪种方式(@Autowired
或这一种)都是 "the standard way"。
如果它仍然报错,我建议您与 JetBrains 联系。