Original: http://www.cnblogs.com/xing901022/p/4168124.html
Today I have a problem, one of my tool classes provides several static methods, and static methods require an instance of another class to provide processing, so the code is written:
1 Class util{2 private static xxx xxx; 3 xxx = Beanutil.getbean ("xxx"); 4 public static void Method1 () {5
xxx.func1 (); 6 } 7 public static void Method2 () {8 xxx.func2 (); 9 } 10}
Here is the way to use the Getbean, get xxx instances, but others say this method is not good, want to inject the way.
But how is static xxx injected?
Surfing the internet to find out a lot of statements, in fact, very simple:
Class util{ private static xxx xxx; public void setxxx (xxx xxx) { this.xxx = xxx; } public void GetXxx () { return xxx; } public static void Method1 () { xxx.func1 (); } public static void Method2 () { xxx.func2 (); } }
Normal configuration injection in XML is OK.
<bean value= "test" class= "X.x.x.util" > <property value= "xxx" ref= "xxx"/></bean>
It is important to note that the auto-generated getter and setter methods, with static qualifiers, need to be removed before they can be used.
[]spring inject static variable