需要注意的是下邊的兩個方法都需要工具類創建實例的時候才會注入。所以不建議在工具類注入實例。因為一般工具類都是直接通過類來使用的。
直接上代碼
方式1:
//可以換成@Configuration,與@Inject配合使用
@Componentpublic
class XXUtils {
//可以換成@Inject/ @Autowired
@Resource
private XXXProperties xxxPropertiesAutowired;
private static XXXProperties xxxProperties;
@PostConstruct
public void init() {
this.xxxPropertiesAutowired = xxxProperties;
}
}
方式2:zs
@Component
public class UrlUtil {
private static RRJConfig rrjConfig;
@Autowired
public void setRRJConfig(RRJConfig rRJConfig) {
UrlUtil.rrjConfig = rRJConfig;
}