0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jakarta EEのInterceptorを基本からまとめてみた【Interceptor入門】

0
Last updated at Posted at 2026-01-11

Jakarta EEの概要

① エンタープライズJavaアプリケーション開発用のAPIの集まり

仕様書・API・TCK(互換性テスト用ツールキット)の3点セット
Java EE → Jakarta EEと名称変更
② 互換性のある実装

  • Jakarta EE Platform(フルスペック)
  • Jakarta EE Web Profile(Webアプリケーション)
  • Jakarta EE Core Profile(マイクロサービス用)

Interceptorの概要

①関心の分離(AOP:アスペクト指向プログラミング)

  • 本質的なビジネスロジック(サービス処理など)と、付加的な共通機能(ログ、権限チェック、トランザクション管理等)を完全に分離して管理できる

②アノテーションベースの適用

  • 独自に作成した「インターセプター・バインディング(@InterceptorBinding)」をメソッドやクラスに付与するだけで、処理を自動的に適用できる

③実行タイミングの制御

  • @AroundInvoke アノテーションを使用することで、対象メソッドが呼び出される直前と直後の両方に処理を挟み込むことが可能

④実行情報の取得(InvocationContext)

  • InvocationContext オブジェクトを通じて、呼び出し元のメソッド名や引数などのメタデータにアクセスでき、柔軟な共通処理を記述できる

⑤コードの冗長性排除

  • 各クラスに重複して記述していた「ロギング」や「例外ハンドリング」を一箇所に集約できるため、コードの可読性と保守性が劇的に向上する

⑥優先順位の管理

  • @Priority アノテーションを使用することで、複数のインターセプターが適用される場合の実行順序を明示的に制御できる

Interceptor前

Interceptor.java
void 処理(){
プロファイルログ
システムログ
監査ログ
ビジネスロジック
プロファイルログ
}

⬇︎

Interceptor後

Interceptor.java
void 処理(){
ビジネスロジック
}

@Interceptor, @Interceptors

App.java
package JakartaEE.interceptor.example.sec01;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class App {
    public static void main(String[] args) {
        Weld weld = new Weld();
        try (WeldContainer container = weld.initialize()){
            container.select(CDIApp.class).get().run();
        }   
    }
}
CDIApp.java
package JakartaEE.interceptor.example.sec01;

import jakarta.inject.Inject;

public class CDIApp {
    @Inject
    private MyService myService;

    public void run() {
        System.out.println("Hello CDIApp sec01");
        this.myService.sayHello("MyService");
    }
}
MyService.java
package JakartaEE.interceptor.example.sec01;

public class MyService {
    public String sayHello(String name) {
        System.out.println("Hello" + name + "!");
    }
}
MyInterceptor.java
package JakartaEE.interceptor.example.sec01;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor

public class MyInterceptor {
    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method invocation");
        Object result = context.proceed();
        System.out.println("MyInterceptor - After method invocation");
        return result;
    }
}

@InterceptorBinding

App.java
package JakartaEE.interceptor.example.sec02;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class App {
    public static void main(String[] args) {
        Weld weld = new Weld();
        try (WeldContainer container = weld.initialize()) {
            container.select(CDIApp.class).get().run();
        }
    }
}
CDIApp.java
package JakartaEE.interceptor.example.sec02;

import jakarta.inject.Inject;

public class CDIApp {
    @Inject
    private MyService myService;

    public void run() {
        System.out.println("Hello CDIApp sec02");
        this.myService.sayHello("MyService");
    }
}
MyService.java
package JakartaEE.interceptor.example.sec02;

//import jakarta.interceptor.Interceptors;

@MyBinding
// @Interceptors(MyInterceptor1.class, MyInterceptor2.class)
public class MyService {
    public String sayHello(String name) {
        System.out.println("Hello" + name + "!");
    }
}
MyInterceptor1.java
package JakartaEE.interceptor.example.sec02;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
public class MyService1 {
    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor1 - before invocation");
        Object result = context.proceed();
        System.out.println("MyInterceptor1 - after invocation");
        return result;
    }
}
MyInterceptor2.java
package JakartaEE.interceptor.example.sec02;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
public class MyService2 {
    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor2 - before invocation");
        Object result = context.proceed();
        System.out.println("MyInterceptor2 - after invocation");
        return result;
    }
}
MyBinding.java
package JakartaEE.interceptor.example.sec02;

import jakarta.interceptor.InterceptorBinding;
import static java.lang.annotation.ElementType.*;

@InterceptorBinding
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface MyBinding {

}

@Priority

App.java
package JakartaEE.interceptor.example.sec03;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class App {
    public static void main(String[] args) {
        Weld weld = new Weld();
        try (WeldContainer container = weld.initialize()) {
            container.select(CDIApp.class).get().run();
        }
    }
}
CDIApp.java
package JakartaEE.interceptor.example.sec03;

import jakarta.inject.Inject;

public class CDIApp {
    @Inject
    private MyService myService;

    public void run() {
        System.out.println("Hello CDIApp sec03");
        this.myService.sayHello("MyService");
    }
}
MyBinding.java
package JakartaEE.interceptor.example.sec03;

import jakarta.interceptor.InterceptorBinding;
import static java.lang.annotation.ElementType.*;

@InterceptorBinding
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface MyBinding {

}
MyInterceptor1.java
package JakartaEE.interceptor.example.sec03;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@MyBinding
@Interceptor
@Priority(Intercepter.Priority.APPLICATION + 1)
public class MyService1 {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor1 - before proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor1 - after proceed");
        return result;
    }
}
MyInterceptor2.java
package JakartaEE.interceptor.example.sec03;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@MyBinding
@Interceptor
@Priority(Intercepter.Priority.APPLICATION + 2)
public class MyService2 {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor2 - before proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor2 - after proceed");
        return result;
    }
}
MyService.java
package JakartaEE.interceptor.example.sec03;

import jakarta.annotation.Priority;

@MyBinding
public class MyService {
    public String sayHello(String name) {
        System.out.println("Hello" + name + "!");
    }
}

@InvocationContext

App.java
package JakartaEE.interceptor.example.sec04;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class App {
    public static void main(String[] args) {
        Weld weld = new Weld();
        try (WeldContainer container = weld.initialize()) {
            container.select(CDIApp.class).get().run();
        }
    }
}
CDIApp.java
package JakartaEE.interceptor.example.sec04;

import jakarta.inject.Inject;

public class CDIApp {
    @Inject
    private MyService myService;

    public void run() {
        System.out.println("Hello CDIApp sec04");
        this.myService.sayHello("MyService");
    }
}
MyBinding.java
package JakartaEE.interceptor.example.sec04;

import jakarta.interceptor.InterceptorBinding;
import static java.lang.annotation.ElementType.*;

@InterceptorBinding
@Retention(RUNTIME)
@Target({ TYPE, METHOD })
public @interface MyBinding {

}
MyInterceptor1.java
package JakartaEE.interceptor.example.sec04;

import jakarta.interceptor.*;
import jakarta.annotation.Priority;

@MyBinding
@Interceptor
@Priority(Intercepter.Priority.APPLICATION + 1)
public class MyInterceptor1 {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor1 - before proceed");
        String clazz = context.getMethod().getDeclaringClass().getName();
        String method = context.getMethod().getName();
        System.out.println("¥tclazz=" + clazz + "method=" + method);

        Object[] params = context.getParameters();
        Arrays.steam(params).forEach(o -> System.out.println("¥tparam=" + o));
        if (params != null && params.length == 1 && params[0] instanceof String str) {
            params[0] = str.toUpperCase();
        }

        Object result = context.proceed();
        System.out.println("MyInterceptor1 - after proceed");
        return result;
    }
}
MyInterceptor2.java
package JakartaEE.interceptor.example.sec04;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@MyBinding
@Interceptor
@Priority(Intercepter.Priority.APPLICATION + 2)
public class MyInterceptor2 {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor2 - before proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor2 - after proceed");
        return result;
    }
}
MyService.java
package JakartaEE.interceptor.example.sec04;

import jakarta.annotation.Priority;

@MyBinding
public class MyService {
    public String sayHello(String name) {
        System.out.println("Hello" + name + "!");
    }
}

@ExcludeClassInterceptors

App.java
package JakartaEE.interceptor.example.sec05;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class App {
    public static void main(String[] args) {
        Weld weld = new Weld();
        try (WeldContainer container = weld.initialize()) {
            container.select(CDIApp.class).get().run();
        }
    }
}
CDIApp.java
package JakartaEE.interceptor.example.sec05;

import jakarta.inject.Inject;

public class CDIApp {
    @Inject
    private MyService myService;

    public void run() {
        System.out.println("Hello CDIApp sec05");
        this.myService.sayHello1("MyService");
        this.myService.sayHello2("MyService");
    }
}
MyInterceptor1.java
package JakartaEE.interceptor.example.sec05;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor1 {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor1 - before proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor1 - after proceed");
        return result;
    }
}
MyInterceptor2.java
package JakartaEE.interceptor.example.sec05;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor2 {
    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor2 - before proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor2 - after proceed");
        return result;
    }
}
MyService.java
package JakartaEE.interceptor.example.sec05;

import jakarta.interceptor.Interceptors;

@Interceptors(MyInterceptor1.class)
public class MyService {
    @interceptors(MyInterceptor2.class)
    public String sayHello1(String name) {
        System.out.println("Hello1" + name + "!");
    }
    @ExcludeClassInterceptors
    @interceptors(MyInterceptor2.class)
    public String sayHello1(String name) {
        System.out.println("Hello2" + name + "!");
    }
}

@AroundConstruct, @PostConstruct, @PreDestroy

App.java
package JakartaEE.interceptor.example.sec06;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class App {
    public static void main(String[] args) {
        Weld weld = new Weld();
        try (WeldContainer container = weld.initialize()) {
            container.select(CDIApp.class).get().run();
        }
    }
}
CDIApp.java
package JakartaEE.interceptor.example.sec06;

import jakarta.inject.Inject;

public class CDIApp {
    @Inject
    private MyService myService;

    public void run() {
        System.out.println("Hello CDIApp sec06");
        this.myService.sayHello("MyService");
    }
}
MyInterceptor.java
package JakartaEE.interceptor.example.sec06;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.interceptor.AroundConstruct;

@Interceptor
public class MyInterceptor {
    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method invocation");
        Object result = context.proceed();
        System.out.println("MyInterceptor - After method invocation");
        return result;
    }

    @AroundConstruct
    public void AroundConstruct(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method proceed");
        context.proceed();
        System.out.println("MyInterceptor - Before method proceed");
    }

    @PostConstruct
    public void postConstruct(InvocationContext context) {
        System.out.println("MyInterceptor - postConstruct()");
    }

    @PreDestroy
    public void preDestroy(InvocationContext context) {
        System.out.println("MyInterceptor - preDestroy()");
    }
}
MyService.java
package JakartaEE.interceptor.example.sec06;

import jakarta.interceptor.Interceptors;

@Interceptors(MyInterceptor.class)
public class MyService {
    public String sayHello(String name) {
        System.out.println("Hello" + name + "!");
    }

    public String sayHello(String name) {
        System.out.println("Hello" + name + "!");
    }
}

@AroundTimeout

App.java
package JakartaEE.interceptor.example.sec08;

import jakarta.ejb.EJBContainer;
import jakarta.naming.Context;

public class App {
    public static void main(String[] args) {
        try(EJBContainer ejbContainer = EJBContainer.createEJBContainer()
        Context context = ejbContainer.getContext();
        HelloBean8 hellobean = (HelloBean8)context.lookup("java:global/classes/HelloBean8!HelloBean8");
        helloBean.sayHello("Enterprise Beans");
    }catch(Exception e)
    {
    e.printStackTrace();
    }
}
HelloBean8.java
package JakartaEE.interceptor.example.sec08;

import jakarta.ejb.Stateless;
import jakarta.ejb.EJB;
import ui.HelloBean;
import jakarta.interceptor.Interceptors;

@Stateless()
@Interceptors({ MyInterceptor.class })
public class HelloBean8 {
    @EJB
    private HelloBean hellobean;

    public void sayHello(String name) {
        System.out.println("Hello " + name + "!");
        this.hellobean.sayHello("Enterprise Beans");
    }
}
MyInterceptor.java
package JakartaEE.interceptor.example.sec08;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.AroundTimeout;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor {
    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method invocation");
        Object result = context.proceed();
        System.out.println("MyInterceptor - After method invocation");
        return result;
    }

    @AroundTimeout
    public void AroundConstruct(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor - Before method proceed");
        return result;
    }
}

@ExcludeDefaultInterceptors

App.java
package JakartaEE.interceptor.example.sec09;

import jakarta.ejb.EJBContainer;
import jakarta.naming.Context;
import javax.naming.NamingException;

public class App {
    public static void main(String[] args) {
        try(EJBContainer ejbContainer = EJBContainer.createEJBContainer()
        Context context = ejbContainer.getContext();
        HelloBean9 hellobean = (HelloBean9)context.lookup("java:global/classes/HelloBean8!HelloBean9");
        helloBean.sayHello("Enterprise Beans");
        Thread.sleep(10000);
    }catch(NamingException|

    Exception e)
    {
        e.printStackTrace();
    }
}
HelloBean9.java
package JakartaEE.interceptor.example.sec09;

import jakarta.ejb.Stateless;
import jakarta.ejb.Schedule;
import jakarta.ejb.Timer;

@Stateless()
public class HelloBean9 {

    public void sayHello(String name) {
        System.out.println("Hello " + name + "!");
    }

    @Schedule(hour = "*", minute = "*", second = "*/10", persistent = false)
    public void timeout(Timer timer) {
        System.out.println("Timeout9 method executed");

    }
}
MyInterceptor.java
package JakartaEE.interceptor.example.sec09;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.AroundTimeout;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor {
    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method invocation");
        Object result = context.proceed();
        System.out.println("MyInterceptor - After method invocation");
        return result;
    }

    @AroundTimeout
    public void AroundConstruct(InvocationContext context) throws Exception {
        System.out.println("MyInterceptor - Before method proceed");
        Object result = context.proceed();
        System.out.println("MyInterceptor - Before method proceed");
        return result;
    }
}

参考サイト

Jakarta EEシリーズその2:Interceptor入門

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?