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