当前位置: 首页 > news >正文

SpringMVC多环境配置的一种方案

# 多环境配置,使用maven的filter

```xml
   <build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/test</path>
                    <port>8081</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <!-- 第一个 resource 配置properties和spring的xml,对指定文本进行过滤替换 -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>spring/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <!-- 第二个 resource 配置,用于处理 mybatis 目录下的资源,不进行过滤 -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>mybatis/**</include>
                </includes>
            </resource>
        </resources>
    </build>



    <profiles>
        <profile>
            <id>
                local
            </id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>local</spring.profiles.active>
                <key>1234</key>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
                <key>123</key>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
                <key>12</key>
            </properties>
        </profile>
    </profiles>
```

# MVC使用代码配置拦截器等内容
需要先注释掉xml里的配置  
```xml
<mvc:default-servlet-handler/>
```

WebMVCConfig.java  
```java
package com.sxwy.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * MVC Config
 *
 * @author: kaixuan
 * @date: 2025/2/11 09:37
 **/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;
    @Resource
    private PlaceholderResourceTransformer placeholderResourceTransformer;
    @Value("${cache.webapp.static.files}")
    private Boolean cacheWebappStaticFiles;

    @Bean
    public HttpMessageConverter<String> stringHttpMessageConverter() {
        return new StringHttpMessageConverter(StandardCharsets.UTF_8);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {

    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        //去除先前注册的默认jackson转换器 去除ES里默认的smile cbor格式等 不需要
        //去除默认的ISO8859-1String converter
        converters.removeIf(converter
                ->
                (converter instanceof MappingJackson2HttpMessageConverter
                        || converter instanceof StringHttpMessageConverter)
        );

        converters.add(0, stringHttpMessageConverter());

        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        CustomObjectMapper objectMapper = new CustomObjectMapper();
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);

    }

    @Override
    public Validator getValidator() {
        return null;
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {

    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {

    }

    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addWebRequestInterceptor(jwtInterceptor)
                .addPathPatterns("/**");// 拦截所有请求
    }

    @Override
    public MessageCodesResolver getMessageCodesResolver() {
        return null;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 对特定路径的静态资源进行特殊处理
        registry.addResourceHandler("/app/**")
                .addResourceLocations("/app/")
                .resourceChain(cacheWebappStaticFiles)
                .addTransformer(placeholderResourceTransformer);


        registry.addResourceHandler("/asstes/js/**")
                .addResourceLocations("/asstes/js/")
                .resourceChain(cacheWebappStaticFiles)
                .addTransformer(placeholderResourceTransformer);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

```
http://www.jsqmd.com/news/33256/

相关文章:

  • 2025 年蒸发器源头厂家最新推荐排行榜:聚焦优质企业,助力工业废水高效处理设备选购热泵刮板蒸发器/多效蒸发器/蒸汽刮板蒸发器公司推荐
  • 097_尚硅谷_经典案例打印字塔
  • 2025年自动遥控割草机价格定制厂家权威推荐:除草机器人/割草机器人/柴油割草机源头厂商精选
  • 2025年粪便干湿分离机源头厂家权威推荐榜单:牛粪干湿分离机厂家/鸡粪干湿分离机/猪粪便干湿分离机源头厂家精选
  • 深入理解import type在模块引入中的作用
  • CentOS7服务器安装Java,MySQL,Nginx,Maven,NodeJs
  • 2025年医院集中供氧系统厂家权威推荐榜单:中心供氧系统工程/空气终端/专业医疗设备带源头厂家精选
  • CentOS7安装异常登录封ip
  • CentOS7服务器部署GitLab
  • postgresql locale区域设置理解
  • CentOS7.9升级gcc
  • 关于模拟赛记录
  • 2025年电动阀门优质制造商推荐排行榜,电动阀门哪家好?
  • 20232423 2025-2026-1 《网络与系统攻防技术》实验四实验报告
  • raft 读请求源码走读
  • 2025年铱星模块生产商新推荐排行榜,专业铱星模块厂家权威测评
  • 跨平台的文件夹映射cifs
  • 2025年工业冷水机品牌供应商/加工厂/批量定制新推荐排行榜白皮书
  • 2025 年护眼食品品牌最新推荐榜单权威发布:聚焦老字号传承与新品牌实力,附选购指南
  • 详细介绍:【MongoDB的RLE压缩数据存储】
  • 模拟赛日志
  • 2025年工作服定制哪家工艺精湛?专业工作服定制生产厂推荐
  • 20251106noip模拟赛
  • 2025年安全检测检验公司排行榜:十大权威机构深度解析
  • 算法社Python基础入门面试题库(新手版含答案) - 指南
  • 2025年安全检测检验公司推荐榜前十名:专业洞察与选择指南
  • 开发管理
  • 错题+trick 集
  • WebStorm 解决无法正确识别Vue3组合式API的问题
  • 激活函数之Tanh