你好,我在一个Spring Boot应用程序中使用了策略模式。我的所有策略都有自动装配构造函数。我是Spring Boot的新手。我不知道如何编写策略类工厂,因为自动装配构造函数已经注入了依赖项。希望能得到任何关于此问题的帮助。
注意:我省略了接口和基类以避免样本混乱。
public class StrategyA implement Strategy {
private DependencyA depA;
private DependencyB depB;
@Autowired
public StragegyA(DependencyA depA, DependencyB depB) {
this.depA = depA;
this.depB = depB;
}
}
public class StrategyB implements Strategy {
private DependencyA depA;
private DependencyB depB;
@Autowired
public StragegyB(DependencyA depA, DependencyB depB) {
this.depA = depA;
this.depB = depB;
}
}
public class StrategyFactory {
public Strategy getStrategy(String strategyName) {
if (name.equals("StrategyA")) {
<b>return StrategyA; //My problem is here
} else {
return StrategyB; // And Here
}
}
}