JAX-RS 2.0 と管理 Bean の統合
Liberty における JAX-RS 2.0 は、 ルート・リソース・クラス、プロバイダー、およびアプリケーション・サブクラスとして、管理 Bean を使用することをサポートします。
- JAX-RS 2.0 と管理 Bean を統合するには、server.xml ファイルの featureManager エレメントに エントリー <feature>managedBeans-1.0</feature> を追加します。
- JAX-RS リソース、プロバイダー、またはアプリケーションとして管理 Bean を使用するには、
@ManagedBean を使用してこれらのクラスにアノテーションを付けます。
例えば、インターセプター管理 Bean フィーチャーを以下のように使用します。
@ManagedBean ("JaxrsManagedBean" ) @Path ("/managedbean" ) public class ManagedBeanResource { public static class MyInterceptor { @AroundInvoke public Object around(InvocationContext ctx) throws Exception { System. out .println("around() called" ); return ctx.proceed(); } } @GET @Produces( "text/plain") @Interceptors(MyInterceptor. class ) public String getIt() { return "Hi managed bean!" ; } }
管理 Bean とともに JAX-RS 2.0 を使用する場合の制約事項
リソース・インジェクションは、Contexts and Dependency Injection (CDI) で管理された以下の JAX-RS コンポーネント・クラスでのみサポートされます。
- アプリケーション・サブクラス
- プロバイダー
- ルート・リソース・クラス
特に、管理 Bean インスタンスを特定の JAX-RS コンポーネント・クラスに注入するには、 このコンポーネント・クラスを CDI Bean として認識して管理できるようにする必要があります。
例えば、printMyName 管理 Bean インスタンスを JAX-RS ルート・リソース・クラスに次のように注入するには、
.WAR file/WEB-INF リポジトリーに空の beans.xml ファイルを追加してください。
@Path ("/managedbean" )
public class ManagedBeanResource {
@Resource(name = "printMyName" )
private PrintMyName printMyName ;
@GET
@Produces( "text/plain")
public String getIt() {
printMyName .print();
return "Hi managed bean!" ;
}
}
@ManagedBean ("printmyname" )
public class PrintMyName {
public void print() {
// TODO Auto-generated method stub
System. out .println("Injection of ManagedBean is successful" );
}
}