首页 > 代码库 > 静态工具类中使用注解注入service(静态方法调用有注解的非静态方法)

静态工具类中使用注解注入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); //调用方法

}

}

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