SpringBoot使用@PostConstruct注解導(dǎo)入配置方式
使用@PostConstruct注解導(dǎo)入配置
通過@PostConstruct注解能夠通過一種更友好的方式將配置進(jìn)行導(dǎo)入
代碼如下:
/**
* 引導(dǎo)類
*
* @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地址(不同服務(wù)器,地址不同)
*/
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的依賴注入實(shí)際上是使用Object.Set()進(jìn)行注入的,Spring是基于對象層面的依賴注入,而靜態(tài)屬性/靜態(tài)變量實(shí)際上是屬于類的。
@PostConstruct和@PreDestroy
@PostConstruct為JavaEE5規(guī)范開始后Servlet中新增@PostConstruct和@PreDestroy被@PostConstruct修飾的方法會在服務(wù)器加載Servlet的時候運(yùn)行,并且只會被服務(wù)器執(zhí)行一次。@PostConstruct 在構(gòu)造函數(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);
}
}
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot中@PostConstruct注解使用小結(jié)
- SpringBoot中@PostConstruct 注解的實(shí)現(xiàn)
- springboot啟動加載CommandLineRunner @PostConstruct問題
- SpringBoot中的@PostConstruct注解詳細(xì)解析
- springboot?@PostConstruct無效的解決
- 淺談SpringBoot中的Bean初始化方法?@PostConstruct
- SpringBoot @PostConstruct和@PreDestroy的使用說明
- SpringBoot @PostConstruct原理用法解析
- SpringBoot中多個PostConstruct注解執(zhí)行順序控制
相關(guān)文章
Spring聲明式事務(wù)@Transactional知識點(diǎn)分享
在本篇文章里小編給大家整理了關(guān)于Spring聲明式事務(wù)@Transactional詳解內(nèi)容,需要的朋友們可以參考下。2020-02-02
spring-spring容器中bean知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于spring-spring容器中bean知識點(diǎn)總結(jié),有需要的朋友們可以學(xué)習(xí)下。2019-08-08
Java連接mysql數(shù)據(jù)庫的詳細(xì)教程(推薦)
這篇文章主要介紹了Java連接mysql數(shù)據(jù)庫的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)
這篇文章主要為大家介紹了使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

