静态工具类中使用注解注入service(静态方法调用有注解的非静态方法)

原文转载:http://blog.csdn.net/p793049488/article/details/37819121

解决方案如下:

/**
*
*/
package cn.common.util;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@Component
public class Utils {


@Autowired
private Service service;
private static Utils utils;

public void setUserInfo(Service service) {
this.service = service;
}

@PostConstruct
public void init() {
  utils = this;
  utils.service = this.service;

}


public static void insertOpeLog(HttpServletRequest req, String str) {

  ServiceBean bean =new ServiceBean();
  utils.service.save(bean); //调用方法

}

}

posted @ 2017-03-24 11:40  Kevin_Zhou_9  阅读(3800)  评论(0编辑  收藏  举报