Announcement Announcement Module
Collapse
No announcement yet.
@autowired not working with a static field Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @autowired not working with a static field

    Hi,
    i tried using @autowired with a static class memeber and it didnt set the property as below
    Code:
    @Autowired	
    private static MyLogger myLogger;
    insted of this, using @autowired with setter method,sets the static property
    Code:
    @Autowired
    public void setMyLogger(MyLogger myLogger) {
    LoggingAdvise.myLogger = myLogger;
    }
    is it because of spring injection happening per object instance and not at class level ?
    or any bug ?
    couldnt check it for prototype scope how its behaving.
    share some light on this
    thanks
    narayan

  • #2
    I don't think you need to declare myLogger as static since by default Spring creates the bean as a singleton bean, so all references to it from any class will use the same instance.

    Your second code snippet doesn't look right. In the setter method, it looks like you're passing myLogger to a static method of the LoggingAdvise class, but never returning anything. So not sure how you would ever use myLogger in this case.

    Comment


    • #3
      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

      Comment


      • #4
        1) Don't use static fields on classes that are Spring singleton beans. As it was already said to you, that is useless and can also lead to problems.

        2) @Autowired on private class fields works through heavy use of java reflection, so it is perfectly normal to me that it doesn't work for a static field.

        Comment


        • #5
          that clarifies things more...
          thanks for replies..appreciate your sharings
          thanks n regards
          narayan

          Comment

          Working...
          X