SpringBoot使用@PostConstruct注解導入配置方式
使用@PostConstruct注解導入配置
通過@PostConstruct注解能夠通過一種更友好的方式將配置進行導入
代碼如下:
/** * 引導類 * * @author zhangzhixiang * @date 2018/09/18 14:51:39 */ @Configuration public class BootstrapConsts { @Value("${file.client.type}") private String fileClientType; @Value("${file.oss.endPoint}") private String endPoint; @Value("${file.oss.accessKeyId}") private String accessKeyId; @Value("${file.oss.accessKeySecret}") private String accessKeySecret; @Value("${file.oss.bucketName}") private String bucketName; @Value("${file.oss.rootDir}") private String rootDir; /** * 文件客戶端類型 */ public static String file_client_type; /** * OSS地址(不同服務器,地址不同) */ public static String end_point; /** * OSS鍵id(去OSS控制臺獲?。? */ public static String access_key_id; /** * OSS秘鑰(去OSS控制臺獲?。? */ public static String access_key_secret; /** * OSS桶名稱(這個是自己創(chuàng)建bucket時候的命名) */ public static String bucket_name; /** * OSS根目錄 */ public static String root_dir; @PostConstruct private void initial() { file_client_type = fileClientType; end_point = endPoint; access_key_id = accessKeyId; access_key_secret = accessKeySecret; bucket_name = bucketName; root_dir = rootDir; } }
使用@PostConstruct注解,完成靜態(tài)對象注入
為什么static對象不可直接使用@Autowired注入?
Spring/SpringBoot正常情況下不能支持注入靜態(tài)屬性(會報空指針異常)。主要原因在于:Spring的依賴注入實際上是使用Object.Set()進行注入的,Spring是基于對象層面的依賴注入,而靜態(tài)屬性/靜態(tài)變量實際上是屬于類的。
@PostConstruct和@PreDestroy
@PostConstruct
為JavaEE5規(guī)范開始后Servlet中新增@PostConstruct和@PreDestroy被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,并且只會被服務器執(zhí)行一次。@PostConstruct 在構造函數(shù)之后執(zhí)行,init()方法之前執(zhí)行。@PreDestroy()
方法在destroy()方法之后執(zhí)行
執(zhí)行順序:Constructor >> @Autowired >> @PostConstruct
使用示例
package com.sijing.codeRecord.httpUtil; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject; @Component public class HttpStaticUtil { @Autowired RestTemplate restTemplate; private static RestTemplate staticRestTemplate; @SuppressWarnings("static-access") @PostConstruct public void staticVarAssignment() { this.staticRestTemplate = restTemplate; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void Post() { HttpEntity requestEntity = null; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.valueOf("application/json")); requestEntity = new HttpEntity(String.format(""), headers); JSONObject response = staticRestTemplate.postForObject("http://www.baidu.com", requestEntity, JSONObject.class); System.out.println(response); } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot以Repository方式整合Redis的方法
這篇文章主要介紹了Springboot以Repository方式整合Redis的方法,本文通過圖文并茂實例詳解給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Spring Security組件一鍵接入驗證碼登錄和小程序登錄的詳細過程
這篇文章主要介紹了Spring Security 一鍵接入驗證碼登錄和小程序登錄,簡單介紹一下這個插件包的相關知識,本文結合示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-04-04Spring 中jdbcTemplate 實現(xiàn)執(zhí)行多條sql語句示例
本篇文章主要介紹了Spring 中jdbcTemplate 實現(xiàn)執(zhí)行多條sql語句示例,可以對多個表執(zhí)行多個sql語句,有興趣的可以了解一下。2017-01-01Spring Boot Actuator監(jiān)控器配置及使用解析
這篇文章主要介紹了Spring Boot Actuator監(jiān)控器配置及使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07PowerJob的QueryConvertUtils工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的QueryConvertUtils工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01