[英]Springboot fails to initialize and inject the third party bean via autowired annotation
I have a problem with my config class. 我的配置类有问题。 The actual issue as per log is :
每个日志的实际问题是:
> Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-05-10 10:47:47.622 ERROR 10728 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field queue in com.example.taskgenerator.Sender required a bean of type 'org.springframework.amqp.core.Queue' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.amqp.core.Queue' in your configuration.
I am using springboot with RabbitMQ a queuing service sdk for the first time. 我第一次在RabbitMQ和排队服务SDK中使用springboot。
package com.example.taskgenerator;
import org.springframework.amqp.core.Queue;
@Configuration
public class TaskGeneratorConfig {
@Bean
public Queue queue() {
return new Queue("simple-queue");
}
}
queue is a dependency for Sender class. queue是Sender类的依赖项。
package com.example.taskgenerator;
import org.springframework.amqp.core.Queue;
@Component
public class Sender {
@Autowired
private Queue queue;
}
Please help. 请帮忙。
Edit : 编辑:
Adding my main Class : 添加我的主要班级:
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
@SpringBootApplication
//@EnableScheduling
//@EnableAsync
public class FileDumpReaderApplication {
@Profile("usage_message")
@Bean
public CommandLineRunner usage() {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
System.out.println("This app uses Spring profiles to control its behaviour.");
System.out.println("Sample usage : java -jar readerapp.jar --spring.profiles.active=hello_world,sender");
}
};
}
@Profile("!usage_message")
@Bean
public CommandLineRunner usageTwo() {
return new AppRunner();
}
public static void main(String[] args) {
SpringApplication.run(FileDumpReaderApplication.class, args);
}
}
And the commandline runner class : 和命令行运行程序类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
public class AppRunner implements CommandLineRunner {
@Autowired
private ConfigurableApplicationContext context;
@Autowired
private Sender sender;
@Override
public void run(String... args) throws Exception {
sender.send();
//context.close();
}
}
Note: all the classes are in the same package. 注意:所有类都在同一包中。
Scan your package -- 扫描您的包裹-
@SpringBootApplication(scanBasePackages={"com.example.taskgenerator"}) and also give annotation(@Component) on AppRunner class. @SpringBootApplication(scanBasePackages = {“ com.example.taskgenerator”}),并在AppRunner类上提供注解(@Component)。
You must need to scan you package so that on application start-up they become part of ApplicationContext. 您必须扫描您的程序包,以使它们在应用程序启动时成为ApplicationContext的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.