springboot 静态方法注入service

时间:2024-10-21 21:06:38

springboot项目中无法使用@Autowired注入sevice,需要手动初始化,初始化后才可使用

@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); //调用方法}
}