Чтобы выполнить задание в TaskExecutor
Мне нужно создать новые задания, реализующие интерфейс Runnable
. Чтобы решить эту проблему, я буду create a new Spring Prototype Bean named Job "on Demand".Как обмениваться экземплярами между прототипами (Spring LoC)
Но в моей заявке Job
имеет два поля LocationChanger
и QueryTyper
. Эти двое должны делиться тем же WebDriver
экземпляром, созданным WebDriverFactory
.
Теперь вопрос заключается в том, как спроектировать это с помощью Spring?
Это соответствующий код:
@Component
@Scope("prototype")
public class Job implements Runnable {
@Autowired
LocationChanger locationChanger;
@Autowired
QueryTyper queryTyper;
@Override
public void run() {
// at this point the locationChanger and
// queryTyper should share the same instance
}
}
@Component
@Scope("prototype")
public class LocationChanger {
@Autowired
@Qualifier(...) // For every new Job Created, the same WebDriver instance should be injected.
WebDriver webDriver
}
@Component
@Scope("prototype")
public class QueryTyper {
@Autowired
@Qualifier(...) // For every new Job Created, the same WebDriver instance should be injected.
WebDriver webDriver
}
public class WebDriverFactoryBean implements FactoryBean<WebDriver> {
@Override
public WebDriver getObject() throws Exception {
return // createdAndPrepare...
}
@Override
public boolean isSingleton() {
return false;
}
}
Спасибо большое!
Update 1: Возможным решением может быть autowire в WebDriver
в Иова только, а затем в @PostConstruct
вводить этот WebDriver к LocationChanger
и QueryTyper
. Но тогда я провожу вручную.
@Component
@Scope("prototype")
public class Job implements Runnable {
@Autowired
LocationChanger locationChanger;
@Autowired
QueryTyper queryTyper;
@Autowired
WebDriver webDriver;
@PostConstruct
public void autowireByHand() {
locationChanger.setWebDriver(this.webDriver);
queryTyper.setWebDriver(this.webDriver);
}
}
// + remove all @Autowired WebDriver's from LocationChanger and QueryTyper
Итак, вы хотите, чтобы «WebDriver» был синглом? – NilsH
Нет, каждый раз, если экземпляр 'Job' создается, этот' Job' и его дочерние элементы должны использовать один и тот же 'WebDriver'. Так что это не Синглтон :( – d0x