spring 提供的配置文件动态扩展入口,通过扩展这个接口可以对spring 进行个性化的配置

    1. /**
    2. * Allows for customization of the application's {@link Environment} prior to the
    3. * application context being refreshed.
    4. * <p>
    5. * EnvironmentPostProcessor implementations have to be registered in
    6. * {@code META-INF/spring.factories}, using the fully qualified name of this class as the
    7. * key.
    8. * <p>
    9. * {@code EnvironmentPostProcessor} processors are encouraged to detect whether Spring's
    10. * {@link org.springframework.core.Ordered Ordered} interface has been implemented or if
    11. * the @{@link org.springframework.core.annotation.Order Order} annotation is present and
    12. * to sort instances accordingly if so prior to invocation.
    13. *
    14. * @author Andy Wilkinson
    15. * @author Stephane Nicoll
    16. * @since 1.3.0
    17. */
    18. @FunctionalInterface
    19. public interface EnvironmentPostProcessor {
    20. /**
    21. * Post-process the given {@code environment}.
    22. * @param environment the environment to post-process
    23. * @param application the application to which the environment belongs
    24. */
    25. void postProcessEnvironment(ConfigurableEnvironment environment,
    26. SpringApplication application);
    27. }

    使用方法:

    1. 一个类实现EnvironmentPostProcessor接口,进行配置;
    2. resources 文件下建一个 META-INF 文件夹,里面建一个application.properties 文件,里面配置接口的全类名即可; ```java package com.alibaba.demo.process;

    import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource;

    import java.util.HashMap; import java.util.Map;

    /**

    • @author demon
    • @date 2020/12/11 15:41 */ public class EnvironmentProcessor implements EnvironmentPostProcessor {

      @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

      1. Map<String, Object> paramMap = new HashMap<>(4);
      2. paramMap.put("server.port","6666");
      3. MapPropertySource mapPropertySource = new MapPropertySource("demon", paramMap);
      4. environment.getPropertySources().addLast(mapPropertySource);

      }

    }

    1. application.properties 文件
    2. ```java
    3. org.springframework.boot.env.EnvironmentPostProcessor=com.alibaba.demo.process.EnvironmentProcessor

    结果
    image.png