Skip to content

Instantly share code, notes, and snippets.

@HydrangeaPurple
Created April 29, 2022 08:40
Show Gist options
  • Save HydrangeaPurple/4b178a4e0426a55ba73dbefbe02235ca to your computer and use it in GitHub Desktop.
Save HydrangeaPurple/4b178a4e0426a55ba73dbefbe02235ca to your computer and use it in GitHub Desktop.
读取tomcat配置文件
<bean class="com.tydic.dep.common.util.props.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:///${catalina.home}/conf/depBssCloudCenter.xml</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
public class ConfigUtils {
public static String getGroupId() {
String property = SystemProperties.getProperty("dep.bss.cloud.groupId");
if (ObjectIsNull.check(property)) {
throw new RuntimeException("groupId 未在tomcat配置文件中配置");
}
return property;
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* 外部配置文件加载类
*
* @author chengyq
* @version V1.0
* @date 2022/4/29 16:27
*/
public class PropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
static Logger logger = LoggerFactory.getLogger(PropertyPlaceholderConfigurer.class);
private Resource[] locations;
private String fileEncoding;
private boolean ignoreResourceNotFound = false;
private Properties props;
public PropertyPlaceholderConfigurer() {
}
public Properties getProperties() {
return this.props;
}
public String getProperty(String key) {
return this.props != null ? this.props.getProperty(key) : null;
}
@Override
public void setLocations(Resource[] locations) {
this.locations = locations;
}
@Override
public void loadProperties(Properties props) throws IOException {
if (this.locations != null) {
PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
for(int i = 0; i < this.locations.length; ++i) {
Resource location = this.locations[i];
if (logger.isDebugEnabled()) {
logger.debug("Loading properties file from " + location);
}
InputStream is = null;
try {
is = location.getInputStream();
if (location.getFilename().endsWith(".xml")) {
propertiesPersister.loadFromXml(props, is);
} else if (this.fileEncoding != null) {
propertiesPersister.load(props, new InputStreamReader(is, this.fileEncoding));
} else {
propertiesPersister.load(props, is);
}
Properties properties = System.getProperties();
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
Iterator<Map.Entry<Object, Object>> it = entries.iterator();
while(it.hasNext()) {
Map.Entry entry = it.next();
if (!props.containsKey(entry.getKey())) {
props.put(entry.getKey().toString(), entry.getValue());
}
}
this.props = props;
new PropertyPlaceholderConfigurer.SystemPropertiesImpl(props);
System.out.println("System env parameter:");
System.out.println("###############################################################################################");
System.out.println(props);
System.out.println("###############################################################################################");
} catch (IOException var13) {
if (!this.ignoreResourceNotFound) {
throw var13;
}
logger.warn("Could not load properties from " + location + ": " + var13.getMessage());
} finally {
if (is != null) {
is.close();
}
}
}
}
}
public Resource[] getLocations() {
return this.locations;
}
@Override
public void setFileEncoding(String fileEncoding) {
this.fileEncoding = fileEncoding;
}
@Override
public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) {
this.ignoreResourceNotFound = ignoreResourceNotFound;
}
static class SystemPropertiesImpl extends SystemProperties {
Properties properties;
public SystemPropertiesImpl(Properties properties) {
this.properties = properties;
this.initialize(this);
}
@Override
public Properties getProperties() {
return this.properties;
}
}
}
import java.util.Properties;
/**
* 加载配置
*
* @author chengyq
* @version V1.0
* @date 2022/4/29 16:30
*/
public abstract class SystemProperties {
private static SystemProperties INSTANCE;
private static final String ERROR_MSG = SystemProperties.class.getName() + " already existed.";
public SystemProperties() {
}
public static SystemProperties getInstance() {
if (INSTANCE == null) {
throw new RuntimeException("没有初始化SystemProperties");
} else {
return INSTANCE;
}
}
public static String getProperty(String key) {
return getInstance().getProperties().getProperty(key);
}
/**
* 返回全量的配置
* @return
*/
public abstract Properties getProperties();
public void initialize(SystemProperties systemProperties) {
if (INSTANCE != null) {
System.err.println(ERROR_MSG);
}
INSTANCE = systemProperties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment