0

i need to inject with @Autowired some variables in a class. This is my situation

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

And this is my xml file

<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>

But if i log the variable hour minutes and allowSystemReboot the result are 0 0 false.

So, where do i fault?

Thanks

EDIT

i modify my classes adding another class and changing the .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..
    }
}

this is the new xml file

<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>

But continuing to get false 0 and 0. arg!!!

ilGiudice
  • 63
  • 1
  • 9
  • 1
    You're creating the `b` member inside the `A` class via `new` so the XML definition (i.e. "repowerjournalpagetask") is unused. – kryger Jul 15 '16 at 14:27
  • The real constructor of B has an argument that i can take from A after some time. There is no way to do what i want without change class A? 'cause a solution would be to set hour minutes and allow in bean of class A and pass it throught costructor to B. – ilGiudice Jul 15 '16 at 14:39
  • @ilGiudice you could use the annotation Value and set the value of some property, and use the Autowired to inject the object, but you cant combine, spring autowired and new instances – cralfaro Jul 15 '16 at 14:42
  • @kryger can you take a look at EDIT? thanks – ilGiudice Jul 19 '16 at 07:30

0 Answers0