Spring项目中静态方法调用注入对象
1、该方法的类要有@Component
注解
@Component
public class OSSUtil {
2、使用@Autowired
注解要调用的注入对象
@Autowired
private AliyunConfig aliyunConfig;
@Autowired
private DhAnnexTableMapper annexTableMapper;
3、创建一个本类的静态对象
private static OSSUtil ossUtil;
4、通过本类的静态对象和@PostConstruct
注解完成初始化
@PostConstruct
public void init() {
ossUtil = this;
ossUtil.annexTableMapper = this.annexTableMapper;
ossUtil.aliyunConfig = this.aliyunConfig;
}
5、使用时通过本类的静态对象获取到注入的对象
ossUtil.aliyunConfig