TaskExecutor
에서 작업을 실행하려면 Runnable
인터페이스를 구현하는 새 작업을 인스턴스화해야합니다. 이 문제를 해결하려면 create a new Spring Prototype Bean named Job "on Demand"을 입력하십시오.프로토 타입 (Spring LoC)간에 인스턴스를 공유하는 방법
그러나 내 응용 프로그램에서 Job
에는 두 개의 필드 LocationChanger
과 QueryTyper
이 있습니다. 이 두 사람은 WebDriver
인스턴스을 WebDriverFactory
으로 만들어 공유해야합니다.
이제 스프링으로 어떻게 설계 할 것인가?
이 관련 코드 :
@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;
}
}
고마워요!
업데이트 1 : 가능한 솔루션은 작업 만 다음 @PostConstruct
에 LocationChanger
및 QueryTyper
받는 사람이 WebDriver를 주입을 WebDriver
를 autowire하기 위해 수 있습니다. 그런데 손으로 철사. 나는 당신의 요구 사항을 이해한다면
@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
아니요, '작업'이 인스턴스화 될 때마다이 '작업'과 그 하위 작업자는 동일한 '웹 드라이브'를 공유해야합니다. 그래서 그것은 Singleton이 아닙니다 : ( – d0x