目录
一,前言
在日常开发过程中,我们时长需要对一些变量或者属性做初始化操作,设置属性值,比如minio的初始化,map的初始赋值等,那么在一个springboot我们争对不同的场景应该如何去做这一操作呢,接下来我们一一道来。
二,正文
非spring bean 的初始化
1. 静态代码块初始化操作
在开发过程中,如果对于一些静态属性的初始化我们可以使用静态代码块来进操作,静态代码块的特点是总的只加载一次,而且只能加载静态变量,所以对于一些静态属性我们可以使用静态代码块来做操作。
package org.example.service.serviceImpl;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
/**
* @author: poet_Dai
* @create: 2025-01-11 16:15
* @Description:
*/
@Service
public class InitTest {
private static ArrayList<String> dataList;
@Autowired
private StudentService studentService;
static {
dataList=new ArrayList<>();
dataList.add("1");
dataList.add("2");
dataList.add("3");
System.out.println("初始化完毕!");
}
}
2. 构造代码块
也有这样一个场景,我需要在每一个类实列化的时候就自动附上初始值,对于这种场景,如果我们每一次使用的时候再去赋值就显得比麻烦了,这时就需要使用构造代码块,特点,每一次实列化的时候执行一次代码块的内容。
package org.example.service.serviceImpl;
import lombok.Data;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
/**
* @author: poet_Dai
* @create: 2025-01-11 16:15
* @Description:
*/
@Service
@Data
public class InitTest {
private static ArrayList<String> dataList;
private LocalDate createDate;
private LocalTime createTime;
@Autowired
private StudentService studentService;
//构造代码块
{
createDate=LocalDate.now();
createTime=LocalTime.now();
}
public static class Test{
public static void main(String[] args) throws InterruptedException {
InitTest initTest = new InitTest();
System.out.println(String.format("final time :%s %s",
initTest.getCreateDate() ,initTest.getCreateTime()));
//延时一秒钟
Thread.sleep(1000);
InitTest initTest1 = new InitTest();
System.out.println(String.format("final time :%s %s",
initTest1.getCreateDate() ,initTest1.getCreateTime()));
}
}
}
执行结果:
spring bean的初始化
当我们需要对spring ioc容器中的bean对象做初始化是,就需要使用spring提供的一些接口和注解,以方便实现初始化功能,从而减少出错的概率。
准备环境
需初始化的类
package org.example.service.serviceImpl;
import lombok.Data;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
/**
* @author: poet_Dai
* @create: 2025-01-11 16:15
* @Description:
*/
@Service
public class InitTest {
private String dateurl;
private String flag;
}
使用的类
package org.example.service.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:04
* @Description:
*/
@Service
public class Tests {
@Autowired
private InitTest initTest;
//测试方法
public void TestInit(){
String url = this.initTest.getDateurl();
Boolean flag = this.initTest.getFlag();
if (null != url && null != flag) {
System.out.println("初始化url成功:" + this.initTest.getDateurl());
System.out.println("初始化flag成功:" + this.initTest.getFlag());
} else {
System.out.println("初始化失败");
}
}
}
测试接口
package org.example.controller;
import org.example.service.serviceImpl.Tests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:11
* @Description:
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private Tests tests;
@GetMapping("/init")
public void Test(){
tests.TestInit();
}
}
1.实现InitializingBean 接口
package org.example.service.serviceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:04
* @Description:
*/
@Service
public class Tests implements InitializingBean {
@Autowired
private InitTest initTest;
@Override
public void afterPropertiesSet() throws Exception {
//初始化属性
this.initTest.setDateurl("http://localhost:8080");
this.initTest.setFlag(true);
}
public void TestInit(){
String url = this.initTest.getDateurl();
Boolean flag = this.initTest.getFlag();
if (null != url && null != flag) {
System.out.println("初始化url成功:" + this.initTest.getDateurl());
System.out.println("初始化flag成功:" + this.initTest.getFlag());
} else {
System.out.println("初始化失败");
}
}
}
测试
2.使用@PostConstruct注解
package org.example.service.serviceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:04
* @Description:
*/
@Service
public class Tests {
@Autowired
private InitTest initTest;
@PostConstruct
public void Init() {
this.initTest.setDateurl("http://localhost:8080");
this.initTest.setFlag(true);
}
public void TestInit() {
String url = this.initTest.getDateurl();
Boolean flag = this.initTest.getFlag();
if (null != url && null != flag) {
System.out.println("初始化url成功:" + this.initTest.getDateurl());
System.out.println("初始化flag成功:" + this.initTest.getFlag());
} else {
System.out.println("初始化失败");
}
}
}
测试:
3.实现CommandLineRunner方法,重写run方法
package org.example.service.serviceImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:04
* @Description:
*/
@Service
public class Tests implements CommandLineRunner {
@Autowired
private InitTest initTest;
@Override
public void run(String... args) throws Exception {
this.initTest.setDateurl("http://localhost:8080");
this.initTest.setFlag(true);
}
public void TestInit() {
String url = this.initTest.getDateurl();
Boolean flag = this.initTest.getFlag();
if (null != url && null != flag) {
System.out.println("初始化url成功:" + this.initTest.getDateurl());
System.out.println("初始化flag成功:" + this.initTest.getFlag());
} else {
System.out.println("初始化失败");
}
}
}
测试:
4.实现ApplicationRunner接口,重写run方法
package org.example.service.serviceImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:04
* @Description:
*/
@Service
public class Tests implements ApplicationRunner {
@Autowired
private InitTest initTest;
@Override
public void run(ApplicationArguments args) throws Exception {
this.initTest.setDateurl("http://localhost:8080");
this.initTest.setFlag(true);
}
public void TestInit() {
String url = this.initTest.getDateurl();
Boolean flag = this.initTest.getFlag();
if (null != url && null != flag) {
System.out.println("初始化url成功:" + this.initTest.getDateurl());
System.out.println("初始化flag成功:" + this.initTest.getFlag());
} else {
System.out.println("初始化失败");
}
}
}
测试:
5.监听ContextRefreshedEvent事件
package org.example.service.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
/**
* @author: poet_Dai
* @create: 2025-01-11 21:04
* @Description:
*/
@Service
public class Tests {
@Autowired
private InitTest initTest;
@EventListener
public void Listen(ContextRefreshedEvent event) {
this.initTest.setDateurl("http://localhost:8080");
this.initTest.setFlag(true);
}
public void TestInit() {
String url = this.initTest.getDateurl();
Boolean flag = this.initTest.getFlag();
if (null != url && null != flag) {
System.out.println("初始化url成功:" + this.initTest.getDateurl());
System.out.println("初始化flag成功:" + this.initTest.getFlag());
} else {
System.out.println("初始化失败");
}
}
}
测试:
三,结语:
以上是我罗列出来关于初始化配置的常用方法,除此之外还有许多其他的方法,但以上的配置基本能够满足日常的开发了,希望你阅读此篇之后对你有所帮助,加油!
标签:初始化,springboot,initTest,springframework,详细,import,org,public From: https://blog.csdn.net/dfd_123456789/article/details/145078870