Spring 引导无法自动装配并且 运行
Spring Boot could not autowire and run
我无法 运行 attached Spring Boot sampler application。它有一个 AMQP 启动器,需要 RabbitMQ。从根本上说,它是一个简单的应用程序,它只是将一条消息发送到绑定了一个队列的 RabbitMQ Exchange。我收到以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.company.messaging.MessageDeliveryManager com.company.exec.Application.messageDeliveryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.messaging.MessageDeliveryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Application.java
package com.company.exec;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
MessageDeliveryManager messageDeliveryManager;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public void run(String... args) throws Exception {
messageDeliveryManager.sendMessage(String message);
}
}
MessageDeliveryManager.java
package com.company.messaging;
public interface MessageDeliveryManager {
void sendMessage(String message);
}
MessageDeliveryImpl.java
package com.company.messaging;
public class MessageDeliveryManagerImpl implements MessageDeliveryManager {
@Value("${app.exchangeName}")
String exchangeName;
@Value("${app.queueName}")
String queueName;
@Autowired
RabbitTemplate rabbitTemplate;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
DirectExchange exchange() {
return new DirectExchange(exchangeName);
}
@Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
public void sendMessage(String message) {
rabbitTemplate.send(queueName, message);
}
}
如果有人可以评论我做错的地方并提出建议,我将不胜感激。
因为你有这样的包树:
com.company.exec
com.company.messaging
并且只使用默认 @SpringBootApplication
,它只是看不到您的 MessageDeliveryManager
及其实现。那是因为 @ComponentScan
(@SpringBootApplication
上的元注释)只扫描当前包及其子包。
要使其正常工作,您应该添加以下内容:
@SpringBootApplication
@ComponentScan("com.company")
或者只是将您的 Application
移动到根包 - com.company
.
我无法 运行 attached Spring Boot sampler application。它有一个 AMQP 启动器,需要 RabbitMQ。从根本上说,它是一个简单的应用程序,它只是将一条消息发送到绑定了一个队列的 RabbitMQ Exchange。我收到以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.company.messaging.MessageDeliveryManager com.company.exec.Application.messageDeliveryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.messaging.MessageDeliveryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Application.java
package com.company.exec;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
MessageDeliveryManager messageDeliveryManager;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public void run(String... args) throws Exception {
messageDeliveryManager.sendMessage(String message);
}
}
MessageDeliveryManager.java
package com.company.messaging;
public interface MessageDeliveryManager {
void sendMessage(String message);
}
MessageDeliveryImpl.java
package com.company.messaging;
public class MessageDeliveryManagerImpl implements MessageDeliveryManager {
@Value("${app.exchangeName}")
String exchangeName;
@Value("${app.queueName}")
String queueName;
@Autowired
RabbitTemplate rabbitTemplate;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
DirectExchange exchange() {
return new DirectExchange(exchangeName);
}
@Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
public void sendMessage(String message) {
rabbitTemplate.send(queueName, message);
}
}
如果有人可以评论我做错的地方并提出建议,我将不胜感激。
因为你有这样的包树:
com.company.exec
com.company.messaging
并且只使用默认 @SpringBootApplication
,它只是看不到您的 MessageDeliveryManager
及其实现。那是因为 @ComponentScan
(@SpringBootApplication
上的元注释)只扫描当前包及其子包。
要使其正常工作,您应该添加以下内容:
@SpringBootApplication
@ComponentScan("com.company")
或者只是将您的 Application
移动到根包 - com.company
.