在启动类添加
@ComponentScan({"com.xx.base.*","com.xx.xxx.*"})
容易忘记
已扫描工具类中使用Spring的Bean报null
使用
public static Demo demo;
@PostConstruct
public void init(){
demo=this;
}
简化版,多个注入用上面一个,调用麻烦点
@Component
public class Demo{
//将要注入的Bean 声明一个静态对象
public static RestfulUtils restfulUtils;
@Autowired
public RestfulUtils restfulUtil;
@PostConstruct
public void init() {
//类中可以调用 restfulUtils 就可以了
restfulUtils = this.restfulUtil;
}
}