记录一下Springboot打成war包部署遇到的坑
问题描述
我先描述下我遇到的问题。在idea上能正常访问。然后打war包到tomcat上跑。启动发现没报错也没有什么日志。访问静态资源是能成功的。但是后台请求报404. 最后因为tomcat的jdk是1.7.而我程序用的是jdk1.8。
指定tomcat 的jdk版本
找到bin下的setclasspath.bat文件;在文件的开始出添加如下代码来设定JAVA_HOME和JRE_HOME的路径。
set JAVA_HOME=D:\Program Files\Java\jdk8\jdk1.8.0_51
set JRE_HOME=D:\Program Files\Java\jdk8\jre8
然后启动日志正常了。但是访问还是不行。最后在我的各种尝试下 ,发现url上要加上两个项目名,请求才能通。原访问tomcat本身就带来一次项目名。在程序配置上去掉另外一个项目名就好。最后pom.xml 去掉了XXX才行(涨姿势了)。后续我讲一下springboot打war包并且从外部获取配置文件的方法。
springboot打war包及读取外部配置
在pom.xml的配置
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
启动类的写法
/**
* 打war包的并且从外部获取配置文件
* 1 继承SpringBootServletInitalizer 重写configure方法
* 2 在pom.xml 1 .<packaging>war</packaging>
* 2.移除嵌入式tomcat插件
* 3 spring-boot-starter-tomcat 中添加 <scope>provided</scope> 代表在调测的时候使用
* 3 在启动类同级目录下新建一个类A,来实现EnvironmentPostProcessor。实现postProcessEnvironment()方法
* 3 在resource下新建META-INF/spring.factories 在文件中添加org.springframework.boot.env.EnvironmentPostProcessor=类A的路径
*/
@Slf4j
@SpringBootApplication
public class MainApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
SpringApplication.run(MainApplication.class, args);
log.info("项目启动");
ApplicationContext context = new AnnotationConfigApplicationContext(MainApplication.class);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MainApplication.class);
}
}
在启动类同级目录下新建一个类来实现EnvironmentPostProcessor接口,重写postProcessEnvironment()方法。
public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor {
private static final String LOCATION="C:\\config\\application.properties";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
File file=new File(LOCATION);
if (file.exists()){
MutablePropertySources mutablePropertySources=environment.getPropertySources();
Properties properties=loadProperties(file);
//指定为第一个。不指定的话不一定生效
mutablePropertySources.addFirst(new PropertiesPropertySource("Config", properties));
}
}
private Properties loadProperties(File f){
FileSystemResource fileSystemResource=new FileSystemResource(f);
try {
return PropertiesLoaderUtils.loadProperties(fileSystemResource);
} catch (IOException e) {
throw new IllegalStateException("加载配置文件失败 路径:"+f.getAbsolutePath());
}
}
}
在resource下新建META-INF/spring.factories .在文件中添加新建类的一个路径。
使得springboot启动的时候会扫描到这个类。
org.springframework.boot.env.EnvironmentPostProcessor=com.atscc.boot.LocalSettingsEnvironmentPostProcessor
大功告成!