SpringBoot使用@PostConstruct注解导入配置方式

网友投稿 1225 2022-11-18

SpringBoot使用@PostConstruct注解导入配置方式

SpringBoot使用@PostConstruct注解导入配置方式

目录使用@PostConstruct注解导入配置使用@PostConstruct注解,完成静态对象注入为什么static对象不可直接使用@Autowired注入?@PostConstruct和@PreDestroy

使用@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桶名称(这个是自己创建bucket时候的命名)

*/

public static String bucket_name;

/**

* OSS根目录

*/

public static String root_dir;

@PostConstruct

private void initial() {

file_client_type = fileClientType;

end_point = http://endPoint;

access_key_id = accessKeyId;

access_key_secret = accessKeySecret;

bucket_name = bucketName;

root_dir = rootDir;

}

}

使用@PostConstruct注解,完成静态对象注入

为什么static对象不可直接使用@Autowired注入?

Spring/SpringBoot正常情况下不能支持注入静态属性(会报空指针异常)。主要原因在于:Spring的依赖注入实际上是使用Object.Set()进行注入的,Spring是基于对象层面的依赖注入,而静态属性/静态变量实际上是属于类的。

@PostConstruct和@PreDestroy

@PostConstruct 为javaEE5规范开始后Servlet中新增@PostConstruct和@PreDestroy被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。@PostConstruct 在构造函数之后执行,init()方法之前执行。

@PreDestroy()方法在destroy()方法之后执行

执行顺序: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.springfrMprKXNGjYamework.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://baidu.com", requestEntity, JSONObject.class);

System.out.println(response);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SAP SEGW 事物码里的 Association 建模方式
下一篇:Cassandra 安装
相关文章

 发表评论

暂时没有评论,来抢沙发吧~