深入理解Spring IOC之扩展篇(八)、环境变量的校验

简介: 深入理解Spring IOC之扩展篇(八)、环境变量的校验

我们在讲refresh方法的那篇中说到了一个prepareRefresh的方法,这个方法主要是记录容器的状态以及对环境变量的校验,我们来看看这个方法的代码:


protected void prepareRefresh() {
  // 1. 记录下容器开始刷新的时间
  this.startupDate = System.currentTimeMillis();
  // 2. 把容器标为激活状态
  synchronized (this.activeMonitor) {
    this.active = true;
  }
  if (logger.isInfoEnabled()) {
    logger.info("Refreshing " + this);
  }
  // 3. ClassPathXmlApplicationContext的时候调的是个空方法,跳过~
  initPropertySources();
  // 4. 调用StandardEnvironment中的validateRequiredProperties方法
  getEnvironment().validateRequiredProperties();
}


我的注释上面写的很明确,3是个空方法,另外,我曾在正篇的第三篇中说过,4这里最后是没有做任何事情的。所以我们可以得出一个结论,在默认情况下,spring容器的启动是不依赖任何的环境变量的。如果我们想增加我们自己的校验环境变量的逻辑应该怎么做呢?这里分为两种情况,对应的做法也不相同,我们分别来看一下:


有人肯定会问自定义校验环境变量的逻辑有什么用,emmm,其实我自己觉得,这个作用还是很大的,比如,你可以让你的应用跑在对应的机器上嘛。环境变量这里的信息还是非常多的,不知道各位读者注意过没有。


第一种情况,你的项目是个普通的Spring而不是SpringBoot项目,此时我们可以这么做:

配置类,仅用作配置


@Configuration
@ComponentScan("com.example.test")
public class Config {
}


负责校验的bean


@Component
public class ConfigCheckBean implements EnvironmentAware {
    private ConfigurableEnvironment environment;
    @Override
    public void setEnvironment(Environment environment) {
        if (environment instanceof ConfigurableEnvironment){
            ConfigurableEnvironment environment1 = (ConfigurableEnvironment)environment;
            this.environment = environment1;
        }
        init();
        this.environment.validateRequiredProperties();
    }
    private void init() {
        this.environment.setRequiredProperties("aaa");
    }
}


测试代码:


public static void main(String[] args) {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Config.class);
}


由于我没有增加任何key为aaa的环境变量,所以此时启动当时是报错了:


1686813805640.png


第二种情况是,当你的项目是个SpringBoot项目时,我们可以这样做:


@SpringBootApplication
@MapperScan(basePackages = {"com.example.studyspringbootdemo.mybatis.mapper"})
public class StudyspringbootdemoApplication {
  public static void main(String[] args) {
      // 将SpringApplication中的ApplicationContext替换,然后启动
    SpringApplication springBootApplication = new SpringApplication(StudyspringbootdemoApplication.class);
    springBootApplication.setApplicationContextClass(MyApplicationContext.class);
    springBootApplication.run();
  }
    // 继承SpringBoot中默认使用的AnnotationConfigServletWebServerApplicationContext并重写initPropertySources方法
  public static class MyApplicationContext extends AnnotationConfigServletWebServerApplicationContext{
    @Override
    protected void initPropertySources() {
        // 因为父类的initPropertySources并不为空,所以为了保持正常运行逻辑必须调用父类方法
      super.initPropertySources();
      // 添加"aaa"的校验
      getEnvironment().setRequiredProperties("aaa");
    }
  }
}


同样的,启动会失败,因为我并没有对应环境变量:


1686813813681.png

目录
相关文章
|
27天前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
27天前
|
XML Java 数据格式
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
这篇文章是Spring5框架的入门教程,详细讲解了IOC容器中Bean的自动装配机制,包括手动装配、`byName`和`byType`两种自动装配方式,并通过XML配置文件和Java代码示例展示了如何在Spring中实现自动装配。
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
|
27天前
|
XML Java 数据格式
Spring5入门到实战------2、IOC容器底层原理
这篇文章深入探讨了Spring5框架中的IOC容器,包括IOC的概念、底层原理、以及BeanFactory接口和ApplicationContext接口的介绍。文章通过图解和实例代码,解释了IOC如何通过工厂模式和反射机制实现对象的创建和管理,以及如何降低代码耦合度,提高开发效率。
Spring5入门到实战------2、IOC容器底层原理
|
27天前
|
XML Java 数据格式
Spring5入门到实战------8、IOC容器-Bean管理注解方式
这篇文章详细介绍了Spring5框架中使用注解进行Bean管理的方法,包括创建Bean的注解、自动装配和属性注入的注解,以及如何用配置类替代XML配置文件实现完全注解开发。
Spring5入门到实战------8、IOC容器-Bean管理注解方式
|
27天前
|
Java Spring 容器
建模底层逻辑问题之以Spring IOC容器为例,使用因果法建模,如何操作
建模底层逻辑问题之以Spring IOC容器为例,使用因果法建模,如何操作
|
29天前
|
安全 Java 开发者
Java 新手入门:Spring 两大利器IoC 和 AOP,小白也能轻松理解!
Java 新手入门:Spring 两大利器IoC 和 AOP,小白也能轻松理解!
27 1
|
29天前
|
XML Dubbo Java
Spring之Ioc容器
该文章主要介绍了Spring框架中的IoC(Inversion of Control,控制反转)容器,包括IoC容器的概念、IoC容器在Spring中的实现以及IoC容器的基础包等内容。
Spring之Ioc容器
|
2月前
|
缓存 Java 程序员
spring IoC 源码
spring IoC 源码
47 3
|
2月前
|
Java Spring 容器
Spring6(二):IoC容器(4)
Spring6(二):IoC容器(4)
17 0