spring 提供的配置文件动态扩展入口,通过扩展这个接口可以对spring 进行个性化的配置
/**
* Allows for customization of the application's {@link Environment} prior to the
* application context being refreshed.
* <p>
* EnvironmentPostProcessor implementations have to be registered in
* {@code META-INF/spring.factories}, using the fully qualified name of this class as the
* key.
* <p>
* {@code EnvironmentPostProcessor} processors are encouraged to detect whether Spring's
* {@link org.springframework.core.Ordered Ordered} interface has been implemented or if
* the @{@link org.springframework.core.annotation.Order Order} annotation is present and
* to sort instances accordingly if so prior to invocation.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @since 1.3.0
*/
@FunctionalInterface
public interface EnvironmentPostProcessor {
/**
* Post-process the given {@code environment}.
* @param environment the environment to post-process
* @param application the application to which the environment belongs
*/
void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application);
}
使用方法:
- 一个类实现EnvironmentPostProcessor接口,进行配置;
- 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) {
Map<String, Object> paramMap = new HashMap<>(4);
paramMap.put("server.port","6666");
MapPropertySource mapPropertySource = new MapPropertySource("demon", paramMap);
environment.getPropertySources().addLast(mapPropertySource);
}
}
application.properties 文件
```java
org.springframework.boot.env.EnvironmentPostProcessor=com.alibaba.demo.process.EnvironmentProcessor
结果