LoginSignup
5
4

More than 5 years have passed since last update.

複数のWebMvcConfigurerの登録

Posted at

あまり機会は無いがspringで複数のWebMvcConfigurerを登録する方法について。

といってもWebMvcConfigurerの実装クラスを二つ用意するだけ。読込順序に関しては@Order@DependsOnで制御する。

pom.xml

pom.xml
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

Configuration

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Order(1)
public class FirstConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        System.out.println("first");
    }
}

@Configuration
@Order(2)
public class SecondConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        System.out.println("second");
    }
}

特に深い意味はないが、適当なメソッドをオーバーライドして読込順序確認用の出力をしている。

実行すると以下のように表示される。

first
second
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4