说我有一个班级
class Service {
private final Repository repository
@Autowired
public Service(Repository repository) {
this.repository = repository
}
}
它自动装配存储库类。说我想改变它也动态添加一些东西,我想通过静态工厂方法来做,同时保持我的依赖。我会尝试这样做:
class Service {
private final Repository repository;
private String field;
@Autowired
public Service(Repository repository) {
this.repository = repository
}
private Service() {}
public static Service of(String field) {
Service service = new Service();
service.setField(field);
return service;
}
private void setField(String field) {
this.field = field;
}
}
但是这段代码不起作用,因为repository
实际上不会被实例化。
有没有办法让我能够同时注入spring依赖项和动态属性?
谢谢!