SpringBoot打war包读取外部配置文件以及遇到的问题

记录一下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

大功告成!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,通常我们将应用程序打成可执行的JAR文件,而不是WAR文件。但是,如果你确实需要将Spring Boot应用程序打WAR文件,可以按照以下步骤进行操作: 1. 在你的Spring Boot项目的pom.xml文件中,将打方式设置为war。在`<packaging>`元素中将值设置为war,如下所示: ```xml <packaging>war</packaging> ``` 2. 在你的主应用程序类上添加`extends SpringBootServletInitializer`,并重写`configure()`方法。这样可以将Spring Boot应用程序注册为一个Servlet。 ```java import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class YourApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(YourApplication.class); } // 程序的入口 public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 3. 在pom.xml文件中添加servlet-api和jsp依赖。这些依赖项是为了支持WAR文件部署和JSP页面的渲染。 ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>javax.servlet.jsp.jstl-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 其他依赖 --> </dependencies> ``` 4. 执行打命令。在命令行中导航到你的项目根目录,并执行以下命令: ``` mvn clean package ``` 5. 在项目的`target`目录下将会生成一个WAR文件,你可以将该WAR文件部署到支持Java应用程序部署的Web服务器上。 希望以上步骤能帮助到你成功打Spring Boot应用程序为WAR文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值