第一步打成war包
1.pom.xml 修改为
<packaging>war</packaging>
2.然后就是依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3.继承启动类
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//Application的类名
return application.sources(DataCenterApplication.class);
}
}
4.在项目的config下建类 MyEnvironmentPostProcessor类实现
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
//tomcat路径
String property = System.getProperty("catalina.home");
System.out.println("catalinahome:"+property);
String path =property+ File.separator+"conf"+File.separator+"application.properties";
File file = new File(path);
System.out.println("Loading local settings from : "+path);
if (file.exists()) {
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
Properties properties = loadProperties(file);
System.out.println(properties.toString());
propertySources.addFirst(new PropertiesPropertySource("Config", properties));
}
}
private Properties loadProperties(File f) {
FileSystemResource resource = new FileSystemResource(f);
try {
return PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException ex) {
throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
}
}
}
4.1 如果你的文件是yml
package com.fyking.etown.config;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
//tomcat路径
String property = System.getProperty("catalina.home");
// property = "D:\\opt\\module\\tomcat8\\apache-tomcat-8.5.89";
System.out.println("catalinahome:" + property);
String path = property + File.separator + "conf" + File.separator + "application-druid.yml";
File file = new File(path);
System.out.println("Loading local settings from : " + path);
if (file.exists()) {
// MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new FileSystemResource(path));
// Properties properties = loadProperties(file);
System.out.println(yaml.toString());
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
propertySources.addFirst(new PropertiesPropertySource("Config", yaml.getObject()));
}
}
private Properties loadProperties(File f) {
FileSystemResource resource = new FileSystemResource(f);
try {
return PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException ex) {
throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
}
}
}
5.在classpath定义一个META-INF文件夹然后在其下面先建spring.factories文件,在其中指定
org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.config.MyEnvironmentPostProcessor
标签:core,读取,springframework,war,File,org,import,new,yml
From: https://www.cnblogs.com/fyking0318/p/17482569.html