mardi 31 mars 2015

Spring 4 @Autowire not binding correctly?

I m trying to Autowire a bean to a implementation but it returns a null pointer Exception.



package org.com.api;
public interface Multiply{
public int multipler(int a, int b);
}
package org.com.core;
import org.com.api.Multiply;
public class MultiplyImpA implements Multiply{
public int multipler(int a, int b){
return a*b;
}
}
package org.com.core;
import org.com.api.Multiply;
public class MultiplyImpB implements Multiply{
public int multipler(int a, int b){
int total = 0;
for(int i = 1; i <=b; i++){
total += a;

}
return total;
}
}

package org.com.Service;
import org.com.api.Multiply;
public Calculator {
@Autowire
private Multiply multiply ;
public int calcMultiply(int a, int b){
return multiply.multipler(a,b);
}
}


In my applicationContext.xml I have added the following



<bean id="multiply" class="package org.com.core.MultiplyImpA" scope="prototype"/>


Now in runtime I get a NullPointerExpection. multiply is null.


for testing purpose I tried this. It works, I understand here I m explicitly getting the Bean. So this means the autowire didnt work ? is there something I m missing ?



Multiply m=(Multiply)factory.getBean("multiply");
System.out.println(m.multiplier(2,4);

Aucun commentaire:

Enregistrer un commentaire