com.github.azbh111.starter.config.AppConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of app-config-spring-boot-starter Show documentation
Show all versions of app-config-spring-boot-starter Show documentation
com.github.azbh111:app-config-spring-boot-starter
The newest version!
package com.github.azbh111.starter.config;
import com.github.azbh111.utils.java.stack.StackTraceManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
/**
*
* @author pyz
* @date 2019/9/17 11:33 上午
*/
@Slf4j
@Service
public class AppConfig {
@Autowired
private AppConfigProperties appConfigProperties;
private String pod;
@PostConstruct
private void init() {
try {
pod = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
log.info("applicationName={}", appConfigProperties.getName());
log.info("runtimeEnvironment={}", appConfigProperties.getRuntimeEnvironment());
log.info("dataEnvironment={}", appConfigProperties.getDataEnvironment());
log.info("pod={}", pod);
log.info("basePackages={}", appConfigProperties.getBasePackages());
log.info("basePackageExecludes={}", appConfigProperties.getBasePackageExecludes());
StackTraceManager i = StackTraceManager.getInstance();
i.getPackagePrefixs().addAll(getBasePackages());
i.getPackageExecludes().addAll(getBasePackageExecludes());
// 工具包
if (!i.getPackageExecludes().contains("com.ruoshui.utils")) {
i.getPackageExecludes().add("com.ruoshui.utils");
}
}
public RuntimeEnvironment getRuntimeEnvironment() {
return appConfigProperties.getRuntimeEnvironment();
}
public DataEnvironment getDataEnvironment() {
return appConfigProperties.getDataEnvironment();
}
/**
* 有一个匹配就返回true
* @param runtimeEnvironments
* @return
*/
public boolean matchAnyEnvironment(RuntimeEnvironment... runtimeEnvironments) {
if (runtimeEnvironments == null) {
return false;
}
for (RuntimeEnvironment enviroment : runtimeEnvironments) {
if (enviroment == appConfigProperties.getRuntimeEnvironment()) {
return true;
}
}
return false;
}
/**
* 项目的唯一键
*
* @return
*/
public String getRuntimeEnvironmentKey() {
return appConfigProperties.getName() + "_" + appConfigProperties.getRuntimeEnvironment().name();
}
/**
* 项目的唯一键
*
* @return
*/
public String getRuntimeEnvironmentKey(String identifier) {
return appConfigProperties.getName() + "_" + appConfigProperties.getRuntimeEnvironment().name() + "_" + identifier;
}
/**
* 相同的dataEnvironment key表示使用的是相同的数据
* @return
*/
public String getDataEnvironmentKey() {
return appConfigProperties.getName() + "_" + appConfigProperties.getDataEnvironment().name();
}
/**
* 相同的dataEnvironment key表示使用的是相同的数据
* @return
*/
public String getDataEnvironmentKey(String identifier) {
return appConfigProperties.getName() + "_" + appConfigProperties.getDataEnvironment().name() + "_" + identifier;
}
/**
* pod的唯一键
*
* @return
*/
public String getPodKey() {
return appConfigProperties.getName() + "_" + appConfigProperties.getRuntimeEnvironment().name() + "_" + pod;
}
/**
* pod的唯一键
*
* @return
*/
public String getPodKey(String identifier) {
return appConfigProperties.getName() + "_" + appConfigProperties.getRuntimeEnvironment().name() + "_" + pod + "_" + identifier;
}
public String getApplicationName() {
return appConfigProperties.getName();
}
public String getPod() {
return pod;
}
public List getBasePackages() {
return appConfigProperties.getBasePackages();
}
public List getBasePackageExecludes() {
return appConfigProperties.getBasePackageExecludes();
}
}