Spring injects a static variable

Source: Internet
Author: User

In general we would like to inject a static variable, as follows:

@Autowired
private static String str;

However, this end result is null.

1, the use of the configuration file method injection

Private Static String from;      Public Static String Getfrom () {        return from ;    }      Public Static void Setfrom (String from) {        = from ;    }     class= "Teststatic" >        <property name= "from" value= "abc"/>    </bean>

2, the use of annotations, but the annotations are written on a non-static method

    Private Static String to;          Public Static String Getto () {        return to ;    }        @Value ("${mail.to}")    publicvoid  Setto (String to) {        = to ;    }

Note: Currently spring annotations do not support static variables and methods, as for reasons: Some say it is because spring is based on the object-level dependency injection, and the use of static variables or classes of what, the expansion of its life cycle, to testing difficulties, so spring does not recommend this. As for any other reason, sorry, no search, cluck, if you have any good reason, can private chat oh, thank you!

Spring injects a 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.