Spring@Enableアノテーションのクイックガイド
1. 概要
Springには、開発者がSpringアプリケーションを簡単に構成できるようにする@Enableアノテーションのセットが付属しています。 これらのアノテーションは、@Configurationアノテーションと組み合わせて使用されます。
この記事では、これらの注釈をいくつか見ていきます。
- @EnableWebMvc
- @EnableCaching
- @EnableScheduling
- @EnableAsync
- @EnableWebSocket
- @EnableJpaRepositories
- @EnableTransactionManagement
- @EnableJpaAuditing
2. @EnableWebMvc
@EnableWebMvc アノテーションは、アプリケーションでSpring MVCを有効にするために使用され、WebMvcConfigurationSupportからSpringMVC構成をインポートすることで機能します。
同様の機能を持つ同等のXMLは
構成は、WebMvcConfigurerを実装する@Configurationクラスによってカスタマイズできます。
@Configuration
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.add(new MyHttpMessageConverter());
}
// ...
}
3. @EnableCaching
@EnableCaching アノテーションは、アプリケーション内でアノテーション駆動型キャッシュ管理機能を有効にし、は、アプリケーションで@Cacheableおよび@CacheEvictアノテーションを使用できるようにします。
同様の機能を持つ同等のXMLは
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(
Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
}
この注釈には、次のオプションもあります。
- mode —キャッシュアドバイスの適用方法を示します
- order —特定のジョインポイントに適用されたときの実行キャッシングアドバイザの順序を示します
- proxyTargetClass —標準のJavaインターフェイスベースのプロキシではなく、サブクラスベース(CGLIB)のプロキシを作成するかどうかを示します
この構成も、CachingConfigurerSupportクラスを実装する@Configurationクラスによってカスタマイズできます。
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
@Bean
@Override
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(
Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new MyKeyGenerator();
}
}
Springキャッシングの使用の詳細については、この記事を参照してください。
4. @EnableScheduling
@EnableScheduling アノテーションは、スケジュールされたタスク機能を有効にし、アプリケーションで@Scheduledアノテーションを使用できるようにします。 同様の機能を持つ同等のXMLは
この構成も、SchedulingConfigurerクラスを実装する@Configurationクラスによってカスタマイズできます。
@Configuration
@EnableScheduling
public class SchedulingConfig implements SchedulingConfigurer {
@Override
public void configureTasks(
ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}
Springスケジューリングの使用の詳細については、この記事を参照してください。
5. @EnableAsync
@EnableAsync アノテーションは、アプリケーションで非同期処理を有効にします。 同様の機能を持つ同等のXMLは
@Configuration
@EnableAync
public class AsyncConfig { ... }
Spring asyncの使用の詳細については、この記事を参照してください。
6. @EnableWebSocket
@EnableWebSocket アノテーションは、Webソケットリクエストの処理を構成するために使用されます。 カスタマイズは、WebSocketConfigurerクラスを実装することで実行できます。
@Configuration
@EnableWebSocket
public class MyConfiguration implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(echoWebSocketHandler(), "/echo").withSockJS();
}
@Bean
public WebSocketHandler echoWebSocketHandler() {
return new EchoWebSocketHandler();
}
}
Spring Websocketの使用の詳細については、この記事を参照してください。
7. @EnableJpaRepositories
@EnableJpaRepositories アノテーションは、アノテーション付き構成クラスのパッケージをスキャンしてリポジトリーを探すことにより、SpringDataJPAリポジトリーを有効にします。
@Configuration
@EnableJpaRepositories
public class JpaConfig { ... }
この注釈に使用できるいくつかのオプションは次のとおりです。
- value — basePackages()属性のエイリアス
- basePackages —注釈付きコンポーネントをスキャンするための基本パッケージ
- enableDefaultTransactions — SpringDataJPAリポジトリのデフォルトトランザクションを有効にするかどうかを設定します
- entityManagerFactoryRef —使用するEntityManagerFactoryBean定義の名前を構成します
8. @EnableTransactionManagement
@EnableTransactionManagement アノテーションは、Springのアノテーション駆動型トランザクション管理機能を有効にします。 同等のXMLは
@Configuration
@EnableTransactionManagement
public class JpaConfig { ... }
Spring Transaction Managementの使用の詳細については、この記事を参照してください。
9. @EnableJpaAuditing
@EnableJpaAuditing アノテーションは、JPAエンティティの監査を有効にします。
@Configuration
@EnableJpaAuditing
public class JpaConfig {
@Bean
public AuditorAware<AuditableUser> auditorProvider() {
return new AuditorAwareImpl();
}
}
Spring Web Socketsの使用の詳細については、この記事を参照してください。
10. 結論
この簡単な記事では、いくつかの @Enable Springアノテーションと、それらを使用してSpringアプリケーションを構成する方法について説明しました。