//必须加上Component注解
@Component
public class ToolUtils{
//初始化注入需要的service
@Autowired
protected Service service;
//静态的工具类
private static ToolUtils toolUtils;
//实例化
@PostConstruct
public void init() {
toolUtils= this;
toolUtils.service= this.service;
}
//使用方式
public static boolean tool1(){
//method1为你的方法名
toolUtils.service.method1();
}
public static boolean tool2(){
//method2为你的方法名
toolUtils.service.method2();
}
通常在静态方法在非静态方法之前加载,此时spring无法为我们直接注入一个静态的service对象,
@PostConstruct在此处就是 让spring对象注入后,将他赋给我们定义的toolUtils,因为toolUtils是静态类,在静态方法中能调用
才疏学浅,如有错误,请指正。



