Using annotations to inject service in static tool classes

Source: Internet
Author: User

http://blog.csdn.net/p793049488/article/details/37819121

——————————————————————————————————————————————————————————————————————————

@Autowired   annotation is injected into a service. But because the tool class method is generally written static

Use the following methods to resolve:

/**  *   */   PackageCn.ffcs.drive.common.util; Importjavax.annotation.PostConstruct; Importjavax.servlet.http.HttpServletRequest; ImportOrg.slf4j.Logger; Importorg.slf4j.LoggerFactory; Importorg.springframework.beans.factory.annotation.Autowired; Importorg.springframework.stereotype.Component; Importcn.ffcs.drive.domain.Admin; ImportCn.ffcs.drive.domain.OpeLog; ImportCn.ffcs.drive.service.IOpeLogService; Importcn.ffcs.zq.util.DateUtils; /*** classname:opelogutils * * Administrator Operation log * *@authorPengyh *@version1.0.0 * @date 2014-07-10 09:04:48 **/@Component Public classOpelogutils {Private StaticLogger Logger = Loggerfactory.getlogger (opelogutils.class); @AutowiredPrivateIopelogservice Opelogservice; Private Staticopelogutils opelogutils;  Public voidSetuserinfo (Iopelogservice opelogservice) { This. Opelogservice =Opelogservice; } @PostConstruct Public voidinit () {opelogutils= This; Opelogutils.opelogservice= This. Opelogservice; }        /*** Perform operation log inbound operation *@paramAdminid Administrator ID *@paramOPEDESC Operation Log Information *@paramCitycode City Code*/       Public Static voidInsertopelog (httpservletrequest req, String opedesc) {Try {              /*** Get administrator information*/Admin Admin=Driveutil.getsessionuser (req); if(Admin! =NULL&& Opedesc! =NULL&&!opedesc.trim (). Equals ("")){                                    //Encapsulating log InformationLogger.info ("begins encapsulating the log information. "); Opelog Opelog=NewOpelog ();                  Opelog.setadminid (Admin.getid ());                  Opelog.setcitycode (Admin.getcitycode ()); Opelog.setopedesc ("Admin id=" +admin.getid () + "action" "+opedesc+" ");                  Opelog.setopetime (Dateutils.getnow ()); Opelog.setisdelete ("0");                                    OpeLogUtils.opeLogService.save (Opelog); Logger.info ("The Save Administrator action Log succeeded with the message" adminid:{},citycode:{},opedesc:{},opetime:{} ",Newobject[]{admin!=NULL? Admin.getid ():NULL, Admin.getcitycode (), Opedesc,dateutils.getnow ()}); }Else{logger.info ("Failed to save operation log with insufficient parameters" adminid:{},citycode:{},opedesc:{},opetime:{} ",Newobject[]{admin!=NULL? Admin.getid ():NULL, admin!=NULL? Admin.getcitycode ():NULL, Opedesc, Dateutils.getnow ()}); }          } Catch(Exception e) {logger.error ("Save the Operation log exception, exception information is:" +e.getmessage (), E); }      }    } 

Using annotations to inject service in static tool classes

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.