0
点赞
收藏
分享

微信扫一扫

面试算法-168-LRU 缓存

莞尔小迷糊 04-11 11:00 阅读 0

1.Spring Boot如何加载外部配置文件?

Spring Boot 设计了一种非常灵活的方式来加载外部配置文件,允许你轻松地管理和调整应用程序的配置而无需改动代码。这些配置文件可以是 properties 文件、YAML 文件、环境变量或命令行参数。Spring Boot 在启动时会按照特定的顺序来加载这些配置,这个顺序确保了特定配置的优先级,使得某些配置可以覆盖其他配置。

配置文件的加载顺序

以下是 Spring Boot 加载外部配置文件的一般顺序,较高的配置覆盖较低的配置:

  1. 命令行参数
  2. 来自 SPRING_APPLICATION_JSON 的属性(环境变量或系统属性中的内联 JSON)
  3. ServletConfig 初始化参数
  4. ServletContext 初始化参数
  5. JNDI 属性
  6. Java 系统属性(System.getProperties()
  7. 操作系统环境变量
  8. 随机生成的配置属性(用于注入随机值如端口号等)
  9. 应用程序在打包时打包到应用内部的配置文件(application.properties 或 application.yml,包括通过 profile 定义的变种)
  10. 在应用程序外部的配置文件(运行时指定的位置,例如通过 --spring.config.location 指定的文件或路径)
  11. 通过配置服务加载的配置属性(例如 Spring Cloud Config)
  12. @Configuration 类上的 @PropertySource 注解
  13. 默认属性(通过 SpringApplication.setDefaultProperties 指定)

如何指定外部配置文件

  • 命令行参数:可以在启动应用时通过命令行参数指定配置属性,如 java -jar app.jar --server.port=8081
  • 应用属性文件:默认情况下,Spring Boot 从应用的 classpath 下的 application.propertiesapplication.yml 文件加载配置。
  • 环境变量:可以设置环境变量来覆盖配置,特别是在云环境或容器环境中非常有用。
  • 外部配置文件位置:使用 --spring.config.location 命令行参数指定外部配置文件的位置,可以是文件路径或目录路径。

使用 Profile

Spring Boot 允许你为不同的环境定义不同的配置文件,称为“profiles”。这些文件命名为 application-{profile}.propertiesapplication-{profile}.yml。你可以通过设置 spring.profiles.active 环境变量来激活特定的配置文件。

通过这些机制,Spring Boot 提供了一种强大而灵活的方式来管理应用配置,使得应用能够适应不同的环境和需求,而无需改动代码。

2. Spring Boot如何实现跨域请求?

在Spring Boot中实现跨域请求(CORS,Cross-Origin Resource Sharing),有几种常用的方法:

1. @CrossOrigin 注解

最简单的方式是在控制器(Controller)或者具体的方法上使用@CrossOrigin注解。这个注解允许你细致地控制哪些请求来源、HTTP方法等被允许访问。

示例:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "http://example.com") // 允许来自http://example.com的跨域请求
public class MyController {

    @GetMapping("/greeting")
    public String greeting() {
        return "Hello, World";
    }
}

2. 全局配置

如果你想为所有的控制器配置CORS策略,可以使用WebMvcConfigurer接口。

示例:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfiguration {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**") // 对所有路径应用规则
                        .allowedOrigins("http://example.com") // 允许来自http://example.com的请求
                        .allowedMethods("GET", "POST", "PUT", "DELETE"); // 允许指定的请求方法
            }
        };
    }
}

3. Spring Security 配置

如果你的项目中集成了Spring Security,你需要在Spring Security的配置中添加CORS配置,因为Spring Security的安全控制会优先于CORS配置生效。

示例:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // 其他配置...
            .cors().and() // 启用CORS配置
            // 其他配置...
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("http://example.com");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

选择适合你需求的方法来实现跨域请求的支持。通常,对于小规模或特定的需求,使用@CrossOrigin注解是最简单直接的方式。而对于需要全局配置跨域策略的情况,通过全局配置或者Spring Security配置会更加合适。

3. Spring Boot的热部署是如何实现的?

Spring Boot 的热部署(也称为热重载)允许开发者在应用运行时更新部分资源(如代码、配置和静态资源),而无需重新启动整个应用。这大大加快了开发过程,因为你可以立即看到更改的效果。Spring Boot 可以通过使用 Spring Loaded 或 Spring Boot DevTools 来实现热部署。

使用 Spring Boot DevTools 实现热部署

Spring Boot DevTools 是 Spring Boot 官方提供的一个模块,旨在提高开发效率。它提供了一系列开发时的便利功能,包括自动重启、模板引擎的缓存禁用等。自动重启是实现热部署的关键特性。

如何配置:
  1. 添加 DevTools 依赖:首先,你需要在你的项目中加入 Spring Boot DevTools 的依赖。如果你使用 Maven,可以在 pom.xml 中添加如下依赖:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

如果你使用 Gradle,则添加如下依赖:

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}
  1. 启用自动重启:仅通过添加 DevTools 依赖,大部分情况下自动重启功能就已经启用。Spring Boot DevTools 默认会监控项目路径下的文件变动,并在检测到更改时重新启动应用。

  2. 配置 IDEA 或其他 IDE:在某些 IDE 中,比如 IntelliJ IDEA,你需要确保「Build Project Automatically」设置被启用(可以在 Preferences -> Compiler 中找到)。对于 Eclipse,你可能需要安装额外的插件来支持自动编译。

工作原理:
  • 当你保存对项目中的文件的更改时,IDE 会自动编译更改过的文件。
  • Spring Boot DevTools 检测到类路径上的更改后,会触发应用的重启。
  • 为了缩短重启时间,DevTools 使用两个类加载器:一个用于第三方库(不变的部分),另一个用于项目的类(会变动的部分)。只有会变动的部分在重启时被重新加载,从而加快了重启速度。

注意事项

虽然热部署提高了开发效率,但它也有一些限制。例如,更改不能改变已有的 bean 定义或是添加新的 bean。对于这种类型的更改,仍然需要重新启动应用。此外,使用 DevTools 时,由于它的工作原理,可能会与某些库产生兼容性问题。因此,虽然它非常适合开发环境,但不建议在生产环境中使用 DevTools。

举报

相关推荐

0 条评论