Spring Boot打成war包引入外部配置文件部署Tomcat

前言:Spring Boot因其特定的配置方式,受到了Java开发者们的热捧,成为了近年来最受欢迎的Java开发框架,当下的Coder如果要新创建项目,首选都是Spring Boot,而且与微服务有着完美的切入口。Spring Boot中内置的Tomcat插件,使得开发者不需要进行额外的Tomcat配置,就可一键启动项目。

Spring Boot可以打包成jar或者war的形式部署,现在生产很大一部分也是通过打包成war包的形式去进行部署,Spring Boot项目 也可以直接打包成可执行的jar或者war包,直接使用java -jar xx.jar/xx.war。

这种方式的话,Spring Boot项目的配置依赖之类的都在jar或war之内,如果需要更改配置,则需要从重新打包重启,不利于运维。

 

下面介绍如何将Spring Boot和配置分离打包成war,放入Tomcat中运行,引入外部配置。

前提条件:一个SpringBoot项目+Tomcat容器(我用的是8.5.53

 

1.先去除spring-boot-starter-web依赖中内置的tomcat容器

<dependency>
    <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

2.添加javax.servlet-api或者tomcat-servlet-api依赖

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

3.修改项目默认启动方式,启动类继承SpringBootServletInitializer类重写configure()方法,也可以新建类继承SpringBootServletInitializer类重写configure()方法,两种方式如下分别如下展示

启动类继承SpringBootServletInitializer类

package com.xiaochun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * 启动类继承SpringBootServletInitializer, 重写configure(SpringApplicationBuilder builder)方法,返回当前启动类
 */
@SpringBootApplication
public class XiaochunApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(XiaochunApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(XiaochunApplication.class);
    }
}

新建类继承SpringBootServletInitializer类

package com.xiaochun;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * 取代原先SpringBoot启动XiaochunApplication.main()方法启动整个项目的方式
 * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
 */
public class XiaochunStartApplication extends SpringBootServletInitializer {

    /**
     * 默认返回return super.configure(builder);
     * 现指向XiaochunApplication.main()启动方法
     * @param builder
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//        return super.configure(builder);
        return builder.sources(XiaochunApplication.class);
    }
}

4.配置文件分离,在pom文件build标签中加入以下配置

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <!--打包时排除src/main/resources下的所有以.properties .yml .xml为后缀的配置文件-->
        <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.yml</exclude>
            <exclude>**/*.xml</exclude>
        </excludes>
    </resource>
</resources>

5.创建外部配置文件xiaochun.properties,我把他放在D盘下,内容如下

xiaochun.version=0.0.1.release
xiaochun.descriprtion=How to package SpringBoor project to war;

6.使用EnvironmentPostProcessor实现程序启动时自动加载自定义属性文件,新建EnvironmentPostProcessorConfig类,指定外部配置文件绝对路径(全路径),内容如下

package com.xiaochun.config;

import
com.xiaochun.enums.ResourcesEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.stereotype.Component; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 配置启动监听器,用于加载外部配置文件 */ @Component public class EnvironmentPostProcessorConfig implements EnvironmentPostProcessor { private Logger logger = LoggerFactory.getLogger(this.getClass()); public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { try {        //ResourcesEnum为枚举类
InputStream inputStream
= new FileInputStream(ResourcesEnum.PROPERTIES_PATH.getPath()); Properties properties = new Properties(); properties.load(inputStream); PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("my",properties); environment.getPropertySources().addLast(propertiesPropertySource); } catch (IOException e) { logger.error("读取自定义配置异常"); } } }

枚举类内容如下

package com.xiaochun.enums;

import lombok.Data;

/**
 * 外部配置文件枚举类,外部配置文件path从这里获取
 */
public enum ResourcesEnum {

    PROPERTIES_PATH("全局配置文件", "D:\\xiaochun.properties");

    private String label;
    private String path;

    ResourcesEnum(String label, String path) {
        this.label = label;
        this.path = path;
    }

    public String getLabel() {
        return label;
    }

    public String getPath() {
        return path;
    }
}

 

7.在resources下新建META-INF\spring.factories,文件内容为如下,用于指定项目启动的监听器,也就是上一步我们自定义的EnvironmentPostProcessorConfig

org.springframework.boot.env.EnvironmentPostProcessor=com.xiaochun.config.EnvironmentPostProcessorConfig

8.打包成war放在tomcat容器的webapps目录下,启动tomcat

9.启动tomcat,访问/home/index接口,验证我们刚配置的那些内容,接口内容如下

package com.xiaochun.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/home")
public class HomeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${xiaochun.version}")
    private String version;

    @Value("${xiaochun.descriprtion}")
    private String description;

    @GetMapping("/index")
    public String index(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("index", "index");
        map.put("version", version);
        map.put("description", description);
        return map.toString();
    }
}

访问结果如下

可以看出,我们的配置是有效的,这样就可以实现不同的环境,我们只需要打包业务相关的代码,不需要去理会配置如何,同一个包,可在测试、生产等不同的环境部署。

posted @ 2020-06-12 22:37  IT咸鱼圈  阅读(4580)  评论(0编辑  收藏  举报