My Autowire可以在任何地方使用,但不能在我的线程上运行。我收到这个错误:
说明
threads.EnvironmentThread中的字段dockerService 需要一个'services.DockerService'类型的bean 无法找到。
动作:
考虑定义类型的bean 您的配置中的“services.DockerService”。
我的代码:
@SpringBootApplication
public class IzyApplication implements CommandLineRunner{
@Autowired
private DockerService dockerService;
public static void main(String[] args) {
SpringApplication.run(IzyApplication.class, args);
}
@Override
public void run(String... args) {
// Call Threads
ApplicationContext context = new AnnotationConfigApplicationContext(ThreadConfig.class);
EnvironmentThread environmentThread = (EnvironmentThread) context.getBean("environmentThread");
environmentThread.setName("Environment Thread");
//Start Them
environmentThread.start();
}
}
线程:运行EnvironmentThread environmentThread = (EnvironmentThread) context.getBean("environmentThread");
@Component
@Scope("prototype")
public class EnvironmentThread extends Thread {
private static final Logger logger = Logger.getLogger(EnvironmentThread.class);
@Autowired
private DockerService dockerService;
@Autowired
private SettingsService settingsService;
@Autowired
private EnvironmentRepository environmentRepository;
@Autowired
private ReportService reportService;
@Override
public void run() {
}
threadconfig.vi可
@Configuration
@ComponentScan(basePackages = {"threads"})
public class ThreadConfig {
}
服务
@Service("dockerService")
public class DockerService {
private static Logger logger;
public DockerClient dockerClient;
@Autowired
private SettingsService settingsService;
// ##################### BUILD CONNECTION WITH DOCKER ####################################
@Singleton
public void buildDockerClient() {
logger = Logger.getLogger(DockerService.class);
try {
//todo remove id
Settings settings = settingsService.getSettings();
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://" + settings.getDockerIP() + ":" + settings.getDockerPort())
.withDockerConfig("/home/user/.docker/config.json")
.build();
dockerClient = DockerClientBuilder.getInstance(config).build();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
答案 0 :(得分:0)
您正在使用ThreadConfig
中指定@ComponentScan(basePackages = {"threads"})
的设置创建应用程序上下文。
DockerService
位于包services
中。该应用程序上下文不知道它,因为您没有告诉它扫描该包。