Hi pgrimard,
this case arised, when i wanted to use my injected bean, in static method of dependent class.for that i had to declare dependency as static.
Code:
package com.mycomp.logic;
import org.apache.log4j.Level;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mycomp.log.MyLogger;
public class BootClass {
private static WorkItOut workItOut;
@Autowired
public void setWorkItOut(WorkItOut workItOut) {
BootClass.workItOut = workItOut;
}
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
BootClass.workItOut.working("Bhati");
try{
BootClass.workItOut.throwing();
}catch(Exception e){
}
}
}
here when i am trying to inject workitOut using setter method @autowired it's working out
but @autowired with field as below is not working.
Code:
public class BootClass {
@Autowired
private static WorkItOut workItOut;
public void setWorkItOut(WorkItOut workItOut) {
BootClass.workItOut = workItOut;
}
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
BootClass.workItOut.working("Bhati");
try{
BootClass.workItOut.throwing();
}catch(Exception e){
}
}
}
but with any non-static field, @autowired at field level is working smooth
can you please explain on this
thanks
Narayan