The solution I finally came up with requires to create a child context. The key point is to specify different child configuration so that parent context is unaware about child component dependencies. The simplest solution is to create a separate java config with enabled component scanning and to place it into a dedicated package.
@Configuration
@ComponentScan
public class TaskConfig {}
public interface TaskFactory {
Task createTask();
}
@Component
public class TaskFactoryImpl implements TaskFactory {
private ApplicationContext parentContext;
@Autowired
public void setParentContext(ApplicationContext parentContext) {
this.parentContext = parentContext;
}
@Override
public Task createTask() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(TaskConfig.class);
context.setParent(parentContext);
context.refresh();
return context.getBean(Task.class);
}
}
}