static方法裡用@Autowire或者@Resource注入的屬性
阿新 • • 發佈:2018-12-18
看程式碼先
@Component//必須有,使當前類成為一個bean物件。 public class CheckUntil { @Autowired private ReadApplicationUntil readApplicationUntil; private static ReadApplicationUntil readApplicationUntils; @PostConstruct public void init() { ReadApplicationUntils = readApplicationUntil; } public static boolean checkSignatures(String signature, String timestamp, String nonce) { String[] strings = new String[]{nonce, ReadApplicationUntils.getToken(), timestamp}; Arrays.sort(strings); StringBuffer stringBuffer = new StringBuffer(); for (String string : strings) { stringBuffer.append(string); } if (SHA1.encode(stringBuffer.toString()).equals(signature)) { return true; } return false; } }
@Component要用到
@Autowired
private ReadApplicationUntil readApplicationUntil; 載入的類
private static ReadApplicationUntil readApplicationUntils; static類
要用到
@PostConstruct//初始化的時候進行的操作註解 public void init() { readApplicationUntils = readApplicationUntil; //這裡需要把static類 和需要載入的類對調,就可以使用static載入readApplicationUntil對應的類方法了 }
@PostConstruct註解作用:Spring允許開發者在受管Bean中使用它。當DI容器例項化當前受管Bean時,@PostConstruct註解的方法會被自動觸發,從而完成一些初始化工作,