为什么没有设置autowire setter值?

时间:2015-11-02 16:11:41

标签: java spring autowired

我有一个这样的课程

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;
}
....

for my xml

...
<bean id="service" class="com.ort.ets.ServiceImpl">
     <property name="required" value="true" />
</bean>
...

很抱歉询问是否有明显的事情,但我被困在这几个小时。当我期待它是真的时,有人可以告诉我为什么要求是假的吗?

编辑:构造函数和方法调用hierachy

编辑: Sysout结果:

1 个答案:

答案 0 :(得分:-1)

在课堂上进行以下更改

public class ServiceImpl {
    private Boolean required = false;

    public void doSomething(Collection someList, int someValue) {
        ...
        //sysout(required) - prints false (expected to be true...)
        if(!required) { 
             execute A
        } else {
             execute B
        }
        ...
    }

    public void setRequired(Boolean required){
        this.required = required
        //sysout(required) - prints true
    }
}

也在xml文件中执行以下更改

<bean id="service" class="com.ort.ets.ServiceImpl">
    <property name="required" value="true" />
</bean>

应该是

<bean id="service" class="com.ort.ets.ServiceImpl">
    <property name="required">
        <value type="java.lang.Boolean">true</value>
    </property>
</bean>

boolean必须替换为对象类型类(java.lang.Boolean