My Autowire works perfectly everywhere but not on my threads. I get this error:
Description:
Field dockerService in threads.EnvironmentThread required a bean of type 'services.DockerService' that could not be found.
Action:
Consider defining a bean of type 'services.DockerService' in your configuration.
My code:
@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();
}
}
Thread : The error get's thrown when running the 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
@Configuration
@ComponentScan(basePackages = {"threads"})
public class ThreadConfig {
}
Service
@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);
}
}