[]spring inject static variable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.