springboot @Autowired @Resource 不支持静态变量的解决方案

阅读更多

借鉴和参考了:http://blog.csdn.net/ohalo/article/details/48999863

 

错误写法:

 

@Autowired
private static OpenSearchProperties openSearchProperties;

 这个写法报错:Autowired annotation is not supported on static fields

 

 

解决方案一:就是不用static (如果你非要用,请看解决方案二)

 

解决方案二:

 

 

private static OpenSearchProperties openSearchProperties;
@Resource(name = "openSearchProperties")
    public void setOpenSearchProperties(OpenSearchProperties openSearchProperties) {
        OpenSearchUtil.openSearchProperties = openSearchProperties;
    }

 其中 @Resource(name = "openSearchProperties") 中的openSearchProperties 就是你要导入的 serice、dao、Component、Configuration等 spring 依赖注入的类

 

你可能感兴趣的:(Autowired,annotation,is,not,supported,on,static,fields,springboot,springmvc)