当前位置: 首页 > news >正文

ApplicationContext接口功能(二)

        ApplicationContext总共有四个拓展能力,上一节已经讲了国际化的能力。今天主要测试匹配资源通配符的能力以及获取环境配置的能力。

一.资源通配符解析

        代码如下:

package com.example.springdemo.demos.a20;import com.example.springdemo.demos.a19.TestFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.boot.env.EnvironmentPostProcessorApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;/*** @author zhou* @version 1.0* @description TODO* @date 2025/5/31 14:37*/
@SpringBootApplication
public class TestApplicationContext {public static void main(String[] args) throws IOException {ConfigurableApplicationContext context = SpringApplication.run(TestApplicationContext.class, args);System.out.println(context.getMessage("hi", null, Locale.CHINA));System.out.println(context.getMessage("hi", null, Locale.ENGLISH));System.out.println(context.getMessage("hi", null, Locale.JAPANESE));//资源通配符解析org.springframework.core.io.Resource[] resources = context.getResources("classpath:application.properties");for (Resource resource:resources){System.out.println(resource);}}
}

       classpath:application.properties会匹配类路径下的所有的application.properties文件。如果匹配磁盘路径需要使用file:再加上路径。

调用的接口如下:

结果如下:

         通过在classpath后面加一个*会在类路径条目中查找所有匹配的资源。(包括jar包)

二.获取环境信息的能力

package com.example.springdemo.demos.a20;import com.example.springdemo.demos.a19.TestFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.boot.env.EnvironmentPostProcessorApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;/*** @author zhou* @version 1.0* @description TODO* @date 2025/5/31 14:37*/
@SpringBootApplication
public class TestApplicationContext {public static void main(String[] args) throws IOException {ConfigurableApplicationContext context = SpringApplication.run(TestApplicationContext.class, args);System.out.println(context.getMessage("hi", null, Locale.CHINA));System.out.println(context.getMessage("hi", null, Locale.ENGLISH));System.out.println(context.getMessage("hi", null, Locale.JAPANESE));//资源通配符解析org.springframework.core.io.Resource[] resources = context.getResources("classpath*:META-INF/spring.factories");for (Resource resource:resources){System.out.println(resource);}//环境信息配置(来源有多个,系统环境变量,properties文件)System.out.println(context.getEnvironment().getProperty("java_home"));System.out.println(context.getEnvironment().getProperty("server.port"));}
}

         通过下面方法可以获取各种来源配置的环境信息,例如系统环境变量或者配置文件。其中键的值可以忽略大小写,系统环境变量配置的JAVA_HOME,但是传入java_home也行。

context.getEnvironment().getProperty()

对应的接口:

结果输出:

http://www.dtcms.com/a/391747.html

相关文章:

  • 多智能体强化学习(MARL)简介:从独立Q学习到MADDPG
  • 【数控系统】第八章 七段式加减速算法
  • 知识蒸馏(KD)详解三:基于BERT的知识蒸馏代码实战
  • 数字化手术室品牌厂家——珠海全视通
  • Linux 冯诺依曼体系结构与进程理解
  • Git GitHub 个人账户创建及链接本地项目教程
  • Leetcode 20
  • 第五章:离家出走
  • RabbitMQ配置项
  • 用html5写一个时区时间查询器
  • deepseek认为明天CSP-J/S初赛的重点
  • 基于Vue的场景解决
  • 浅谈 Sui 的区块链隐私解决方案
  • ETF期权交易的基础知识是什么?
  • 连接管理模块的实现
  • AI 的耳朵在哪里?—— 语音识别
  • 微博舆情大数据实战项目 Python爬虫+SnowNLP情感+Vue可视化 全栈开发 大数据项目 机器学习✅
  • Dify笔记
  • 高精度维文OCR系统:基于深度学习驱动的实现路径、技术优势与挑战
  • 使用Python+Selenium做自动化测试
  • GESP C++ 三级 2025年6月真题解析
  • Linux系统多线程的互斥问题
  • Python 之监控服务器服务
  • el-select 多选增加全部选项
  • Day24 窗口操作
  • 5. Linux 文件系统基本管理
  • 【MySQL】GROUP BY详解与优化
  • 深度学习:DenseNet 稠密连接​ -- 缓解梯度消失
  • Linux DNS 子域授权实践
  • 团体程序设计天梯赛-练习集 L1-041 寻找250