我有這樣一類這個爲什麼autowire setter值未設置?
public class ServiceImpl {
private boolean required = false;
public ServiceImpl (Log log) {
this.db = Registry.getDatabase(this);
this.log = log;
rt = new Table(log, db);
it = new ItemTable(log, db, rt);
gt = new GroupTable(log, db);
}
public ServiceImpl() {
this.db = Registry.getDatabase(this);
log = Registry.getLog(this);
rt = new Table(log, db);
it = new ItemTable(log, db, rt);
gt = new GroupTable(log, db);
}
public void trySomething(Collection someList, int value) {
for(int i=0; true; i++){
try{
doSomething(someList,value);
return;
} catch (..) {
...
}
}
}
public void doSomething(Collection someList, int someValue) {
...
//sysout(required) - prints false (expected to be true...)
if(!required) {
execute A
} else {
execute B
}
...
}
public setRequired(boolean required){
this.required = required
//sysout(required) - prints true
}
二班(上一級)
abstract class Synch {
...
private ServiceImpl service;
protected synch(Log log, ServiceImpl service, Keys key) {
this.log = log;
this.key = key;
this.service = service;
}
....
void execute(){
try{
service.trySomething(someList,someValue);
}..
protected abstract void service() throws LockException(){
return service;
}
....
我的XML
...
<bean id="service" class="com.ort.ets.ServiceImpl">
<property name="required" value="true" />
</bean>
...
不好意思問,如果事情是顯而易見的,但我堅持在這幾個小時。有人可以告訴我爲什麼要求是錯誤的,當我期待它是真實的嗎?
編輯:構造函數和方法調用層次結構
編輯: 系統輸出結果:
真正
假
當你有兩個打印,你看到他們同時打印(順序錯誤)或者'setRequired'永遠不會調用(在這種情況下,它應該是錯誤的,因爲bean配置失敗) – zapl
首先設置打印值(值爲true),然後打印第二個(false)。 Setter被調用。但不知何故被默認覆蓋。不知道我能做些什麼來修復它 – logger
這將意味着你沒有使用正確初始化的bean實例,而是你自己新創建的那個實例! – zapl