文章目录
- 1.common-env-starter模块
- 1.目录结构
- 2.DotenvEnvironmentPostProcessor.java 在${xxx}解析之前执行,提前读取配置
- 3.EnvProperties.java 这里的path只是为了代码提示
- 4.EnvAutoConfiguration.java Env模块自动配置类
- 5.spring.factories 自动配置和注册EnvironmentPostProcessor
- 6.pom.xml
- 2.common-env-starter-demo模块
- 1.目录结构
- 2.pom.xml
- 3.application.yml 配置测试的数据
- 4.EnvConfig.java
- 5.EnvApplication.java 启动类
- 6.测试
1.common-env-starter模块
1.目录结构
2.DotenvEnvironmentPostProcessor.java 在${xxx}解析之前执行,提前读取配置
package com.sunxiansheng.env.processor;
import io.github.cdimascio.dotenv.Dotenv;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
public class DotenvEnvironmentPostProcessor implements EnvironmentPostProcessor{
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String dotenvPath = environment.getProperty("sun-rays.env.path");
if (dotenvPath != null) {
Dotenv dotenv = Dotenv.configure()
.directory(dotenvPath)
.filename(".env")
.load();
dotenv.entries().forEach(entry ->
environment.getSystemProperties().put(entry.getKey(), entry.getValue())
);
System.out.println("Loaded .env from path: " + dotenvPath);
} else {
System.err.println("sun-rays.env.path not configured!");
}
}
}
3.EnvProperties.java 这里的path只是为了代码提示
package com.sunxiansheng.env.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "sun-rays.env")
@Data
public class EnvProperties {
private String path;
}
4.EnvAutoConfiguration.java Env模块自动配置类
package com.sunxiansheng.env.config;
import com.sunxiansheng.env.config.properties.EnvProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@EnableConfigurationProperties({EnvProperties.class})
@Slf4j
public class EnvAutoConfiguration {
@PostConstruct
public void logConfigSuccess() {
log.info("EnvAutoConfiguration has been loaded successfully!");
}
}
5.spring.factories 自动配置和注册EnvironmentPostProcessor
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.env.config.EnvAutoConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
com.sunxiansheng.env.processor.DotenvEnvironmentPostProcessor
6.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>common-env-starter</artifactId>
<dependencies>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
</dependency>
</dependencies>
</project>
2.common-env-starter-demo模块
1.目录结构
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-demo</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>common-env-starter-demo</artifactId>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>common-env-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
3.application.yml 配置测试的数据
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-common-demo/common-env-starter-demo/logs
module: sunrays-common-demo/common-env-starter-demo
env:
path: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-common-demo/common-env-starter-demo
env:
test: ${ENV_TEST}
4.EnvConfig.java
package com.sunxiansheng.env.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@Slf4j
public class EnvConfig {
@Value("${env.test}")
private String test;
@PostConstruct
public void init() {
log.info("test={}", test);
}
}
5.EnvApplication.java 启动类
package com.sunxiansheng.env;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EnvApplication {
public static void main(String[] args) {
SpringApplication.run(EnvApplication.class, args);
}
}
6.测试