广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot使用@PostConstruct注解导入配置方式
  • 679
分享到

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

2024-04-02 19:04:59 679人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

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

使用@PostConstruct注解导入配置

通过@PostConstruct注解能够通过一种更友好的方式将配置进行导入

代码如下:



@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;
    
    public static String end_point;
    
    public static String access_key_id;
    
    public static String access_key_secret;
    
    public static String bucket_name;
    
    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注解,完成静态对象注入

为什么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.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);
 } 
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringBoot使用@PostConstruct注解导入配置方式

本文链接: https://www.lsjlt.com/news/158718.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作