自动弹簧原始值

时间:2016-07-15 14:15:58

标签: java spring autowired

我需要在类中注入@Autowired一些变量。 这是我的情况

    class A {
        private B b = new B();
       ... toDo ..
    }

    class B {
        // Variables that i need to inject @Autowired
        private int hour;
        private int minutes;
        private boolean allowSystemReboot;

        // setter and getter method for the 3 var

        public B() {
           ..toDo..
        }
    }

这是我的xml文件

<beans>
    <context:annotation-config />  
    <bean id="rpwTask" class="package.A" >
        <property name="maxDelay" value="300" />
        <property name="rate" value="60" />
    </bean>
    <bean id="repowerjournalpagetask" class="package.B" >
        <property name="hour" value="4" />
        <property name="minutes" value="15" />
        <property name="allowSystemReboot" value="true" />
    </bean>
</beans>

但是如果我记录变量小时分钟和allowSystemReboot,则结果为0 0 false。

那么,我哪里错了?

由于

修改

我修改我的类添加另一个类并更改.xml

class A {
    private B b = new B();
    // Variables that i need to inject @Autowired
    private int hour;
    private int minutes;
    private boolean allowSystemReboot;

    // setter and getter method for the 3 var

   ... toDo ..
   public B createB() {
       B b = new B(hour, minutes, allowSystemReboot);
   }
}


class B {
    private int hour;
    private int minutes;
    private boolean allowSystemReboot;

    // setter and getter method for the 3 var

    public B(int h, int m boolean asr) {
       hour = h;
       minutes = m;
       allowSystemReboot = asr;
       ..toDo..
    }
}

这是新的xml文件

<beans>
    <context:annotation-config />  
    <bean id="rpwTask" class="package.A" >
        <property name="hour" value="4" />
        <property name="minutes" value="15" />
        <property name="allowSystemReboot" value="true" />
        <property name="maxDelay" value="300" />
        <property name="rate" value="60" />
    </bean>
</beans>

但继续得到假0和0. arg !!!

0 个答案:

没有答案