0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Java Interceptorの有効化

Posted at

JavaEEのインターセプターを有効にする方法について

インターセプターが有効になっていないなーと思ったら、Priorityアノテーションを忘れていました。
調べるとbeans.xmlで設定する方法もあるようで、違いはなんだろうと思ったのですけど、特にないようですね。
beans.xmlで定義するとソースコードを修正せずに有効にできたり無効にしたりできる程度ですね。

方法は2つあります

  1. interceptorに@Priorityアノテーションをつける
  2. beans.xmlにinterceptorを定義する

1. Interceptorに@Priorityアノテーションをつけるサンプル

TraceLogInterceptor.java
package sample;

@InterceptorBinding
@Target({Element.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TraceLogInterceptor {
}
TraceLogImpl.java
package sample;

@Priority(Interceptor.Priority.APPLICATION)
@TraceLogInterceptor
@Interceptor
public class TraceLogImpl {
}

2. beans.xmlにInterceptorを定義するサンプル

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       bean-discovery-mode="annotated">
    <interceptors>
        <class>sample.TraceLogInterceptor</class>
    </interceptors>
</beans>
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?