日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring environment_SpringBoot实战8-Spring基础-应用环境

發布時間:2025/3/15 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring environment_SpringBoot实战8-Spring基础-应用环境 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

接上篇《SpringBoot實戰7-Spring基礎-Bean的生命周期》的學習,本文學習Spring的一個重要的概念Environment-應用環境。

2.7 應用環境 - Environment

Spring為我們提供了一個接口Environment用來代表當前運行應用的環境,這個環境包含兩個部分:

  • Profile:指的是一組命名的、定義在一起的Bean。我們通常為不同的應用場景(生產、開發,測試等)定義。
  • Property:指的是配置屬性,我們可以從properties文件、JVM系統屬性、操作系統環境變量等外部來獲得配置屬性。

2.7.1 場景 - @Profile

我們可以通過@Profile注解指定當前的運行場景,@Profile可以和@Component等、@Configuration和@Bean一起使用,當然也分別限制了@Profile起效的Bean的分組。

下面使用需要顯示不同操作系統的列表命令的Bean:

public class CommandService { private String listCommand; public CommandService(String listCommand) { this.listCommand = listCommand; } public void list(){ System.out.println("當前系統下列表命令是:" + listCommand); }}

在開發環境Windows下的配置為:

@Configuration@Profile("dev")public class WindowsProfileConfig { @Bean CommandService commandService(){ return new CommandService("dir"); }}

在生產環境Linux下的配置為:

@Configuration@Profile("production")public class LinuxProfileConfig { @Bean CommandService commandService(){ return new CommandService("ls"); }}

當我們配置好了兩種不同場景下的Profile,我們需要在應用中配置哪個是激活的Profile,手動配置應該是像下面這樣:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getEnvironment().setActiveProfiles("production"); context.scan("top.wisely");context.refresh();

但我們使用了Spring Boot,我們只需在application.properties文件配置:

spring.profiles.active=production

我們在JavaConfig里用CommandLineRunner分別運行將Profile置成production和dev:

@BeanCommandLineRunner profileClr(CommandService commandService){ return args -> commandService.list();}

2.7.2 屬性配置 - @PropertySource

Spring的Environment的屬性是由PropertySource組成的,我們可以通過@PropertySource指定外部配置文件的路徑,這些配置文件的屬性都會以PropertySource的形式注冊到Environment中,@PropertySource支持xml和properties格式,不支持Spring Boot下的YAML格式。

如我們現在添加了2個外部配置文件:

  • author.properties
author.name=wyf
  • book.properties
book.name=spring boot in battle

我們可以用一個配置類來接受這兩個文件的配置:

@Configuration@PropertySources({ @PropertySource("classpath:author.properties"), @PropertySource("classpath:book.properties")}) //1public class ExternalConfig { Environment env; public ExternalConfig(Environment env) { //2 this.env = env; } @Value("${book.name}") //3 private String bookName; public void showEnv(){ System.out.println("作者名字是:" + env.getProperty("author.name")); //4 System.out.println("書籍名稱是:" + bookName); }}
  • 多個外部配置可以用@PropertySources,若只有一個可以只使用@PropertySource("classpath:book.properties");
  • 注入Environment的Bean,因只有一個參數,可省略@Autowired;
  • 可以@Value注解獲得Environment中的屬性,@Value的使用在Spring EL一節有更詳細的講解;
  • 外部配置的屬性都已經在Environment注冊,可以直接獲取。
  • 下一篇《SpringBoot實戰9-Spring基礎-條件配置@Conditional》

    總結

    以上是生活随笔為你收集整理的spring environment_SpringBoot实战8-Spring基础-应用环境的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。