Normal static class method acquires spring injected been entity

Source: Internet
Author: User

Originally wanted to get the spring annotations in the static method after the been, found to add the static keyword to the been field, start Tomcat error-------to the effect that static fields cannot be annotated.

Then Google, the workaround is as follows:

Package Com.sondon.dev_info.controller;import Javax.annotation.postconstruct;import Javax.annotation.PreDestroy; Import Javax.annotation.resource;import Org.springframework.stereotype.controller;import com.sondon.dev_ Info.entity.devinfo;import com.sondon.dev_info.service.templatemanager;/** * @Project: Dev_info_web * @Package: Com.sondon.dev_info.controller * @Class: Dev_info.java * @Company Guangzhou Xun Network Technology Co., Ltd. * @Author: Cai Wen * @DateTime: April 24, 2015 PM 1 : 39:58 * @Blog: http://blog.csdn.net/caiwenfeng_for_23 * @Description: {} */@Controllerpublic class Dev_infocontroller { @Resourcepublic templatemanager<devinfo> templatemanager;private Static Dev_infocontroller Dev_infocontroller /** * Constructor method executes after calling init () */@PostConstructpublic void init () {System.out.println ("I ' m init method using @PostConstrut.  ..."); Dev_infocontroller = This;dev_infocontroller.templatemanager = This.templatemanager;} /** * servlet is destroyed before calling Dostory () */@PreDestroy public void Dostory () {System.out.printlN ("I ' m destory method using @PreDestroy ..."); } public static void Add_devinfo (DevInfo DevInfo) {//Boolean result=false;dev_infocontroller.templatemanager.add ( DevInfo);//return result;} /** * @param Templatemanager * The Templatemanager to set */public void Settemplatemanager (templatemanager<d Evinfo> templatemanager) {this.templatemanager = Templatemanager;}}

From the above you can see the use of@PostConstruct, and then in the static methodAdd_devinfo (DevInfo DevInfo) called theTemplatemanager


Do not know @postconstruct @PreDestroy classmates, you can read the following article:

Annotated @postconstruct and @predestroy explanation and examples

Normal static class method acquires spring injected been entity

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.