我正在嘗試改進我寫的一些代碼的結構。我想重寫一個服務的方法,但需要另一個參數/對象用於新功能。在普通的Java中,我會通過使所需的對象成爲類的屬性並在構造函數中初始化來解決此問題。但是因爲這是一個bean,所以構造函數沒有參數。使用其他參數覆蓋無狀態Spring服務的方法
@Service("testService")
public class TestService extends AbstractService {
@Override
public String perform(String original) {
return original + extra; //<-- What's the best way to get
// 'extra' here from the controller
// without going stateful?
}
}
######
public abstract class AbstractService {
public String perform(String original);
}
我覺得我的問題最好由上面的代碼和註釋來描述。我如何讓該對象可以從該方法訪問,而無需向該類添加屬性?
我對春天很新,所以隨時可以告訴我我的方式偏離軌道。
http://www.baeldung.com/constructor-injection-in-spring – Shibashis
爲什麼不只是重構併爲AbstractService中的方法添加額外的參數。 – Strelok