2016-07-01 5 views
1

2つの構成を1つの構成にマージしたいと考えています。ここでは、彼らが現在どのように見えるかです:WebMvcConfigurerAdapterとWebSecurityConfigurerAdapterを1つの@Configurationに拡張する@Configurationsをマージする方法はありますか?

WebMvcConfigurerAdapter私は春のクラス階層を見て、これらがどのような方法で関連しているクラスを拡張していないことを見てきました

@EnableAsync 
@EnableScheduling 
@EnableTransactionManagement 
@EnableAspectJAutoProxy(proxyTargetClass = true) 
@ComponentScan(
     basePackages = {"..."}) 
@Import({ 
     ... 
}) 
@PropertySources({ 
     @PropertySource("..."), 
     @PropertySource(
       ignoreResourceNotFound = true, 
       value = "...") 
}) 
@EnableWebSecurity 
@Configuration 
public class AdminConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     //... 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     //... 
    } 

@Configuration 
@EnableWebMvc 
@Import(...) 
public class WebApplicationConfig extends WebMvcConfigurerAdapter { 


    @Override 
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) { 
    //... 
    } 

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

    @Override 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 
     //... 
    } 

    @Override 
    public void configurePathMatch(PathMatchConfigurer configurer) { 
     //... 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     //... 
    } 

WebSecurityConfigurerAdapter。それらを置き換える注釈がありますか?これは、クラスを拡張するのではなく、インタフェースを実装することで解決できますか?

私がこれらのファイルをマージしたいのは、私が自分のアプリケーションをテストしているときにどのファイルを使うか考える必要がないように単一の設定をしたいからです。

答えて

3

私はそれらを同じクラスに入れることを推奨しません。 1つのクラスですべてのロジックを詰め込まないのと同じ理由で、これはお勧めできません。構成を簡潔かつ重要なものにしておきます。さらに、これは実際には時々循環Beanの参照を伴って厄介になることがあります。

私はあなたの問題を解決するために組成物をやってお勧めします:

@Configuration 
@Import({AdminConfig.class, WebApplicationConfig.class}) 
public class TheConfig {} 

今、あなただけのTheConfigを参照することができます。また

、これが唯一のテストについてあなたは、例えばmeta annotationに構成を置くことができる場合:

@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
@ContextConfiguration(classes = {..}) 
public @interface TheTests { } 

最後にあなたが本当にこのような何かを行うことができ、単一のクラスを使用する場合:

@EnableWebMvc 
@Import(...) 
@EnableAsync 
@EnableScheduling 
@EnableTransactionManagement 
@EnableAspectJAutoProxy(proxyTargetClass = true) 
@ComponentScan(
     basePackages = {"..."}) 
@Import({ 
     ... 
}) 
@PropertySources({ 
     @PropertySource("..."), 
     @PropertySource(
       ignoreResourceNotFound = true, 
       value = "...") 
}) 
@EnableWebSecurity 
@Configuration 
public class TheConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer { 
    ... 
} 

さらにこの問題は、WebMvcConfigurerAdapterが提供されているため、APIのすべてのメソッド(必要なもののみ)を実装する必要はありません。さらに、メソッドを追加する可能性のあるインタフェース内の変更を防ぐことができます。

0

最も簡単で慣用的なアプローチは、トップレベルに静的ネストされたクラス構成器@Configurationクラスそれぞれを作ることである。

@Configuration 
public class TestConfig { 
    @Configuration 
    @EnableWebSecurity 
    public class Security extends WebSecurityConfigurerAdapter { 
     ... 
    } 

    ... 
} 

インポートトップレベルクラスを、そして春も、ネストされた構成をインポートします。

関連する問題