Paul Paul Public Builder & 全栈工程师
search
arrow_back 返回标签拓扑

配置

共 1 篇。

2022-03-20 · 约 2 分钟

SpringBoot外部化配置

前言 在前面的《SpringBoot可执行jar原理》里面我提到过SpringBoot的几个核心功能,而今天的主角外部化配置(Externalized Configuration)正是其中之一。 Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, and command-line arguments. Spring官方给的解释是“外部化”你的配置文件,能让同一套的程序在不同的环境下工作。官方还给出properties、YAML、环境变量、还有command-line参数这些例子。 首先,这个外部化的配置肯定不是指我们自定义配置文件,而是指SpringBoot的系统配置文件。也就是我们常说的application.properties(或spring.yaml、spring.yml)。但是,“外部化”究竟是什么意思?SpringFramework和SpringBoot的配置到底有何差异? 差别对别 文本内容 SpringFramework xml content_copy 复制代码 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:websocket="http://www.springframework.org/schema/websocket" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"> <!-- <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。它提供了数据绑定支持,读取json的支持 --> <mvc:annotation-driven /> <!-- 配置jsp视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value=""/> <property name="suffix" value=".jsp"/> </bean> </beans> 这里我们看到了一些熟悉的配置,注解驱动、Componenet扫描、jsp相关配置等等。这些是我们在使用SpringFramework时经常看到的配置,可是在SpringBoot中我们似乎看不到了(忽略掉配置的内容,这里只是举个例子,想告诉你springFramework是通过这种方式来配置一个bean)。

阅读全文 arrow_forward