インジェクション対象外にするクラス・パッケージの指定

【前提条件】

[環境]

【概要】

CDI1.1から追加されたbeans.xmlのscanについてです。

CDI1.0ではインジェクションするインターフェイスに紐付くクラスが
複数あった場合、デフォルトでインジェクションするクラスを
明示的に指定する必要がありました。

scanタグの配下には除外するクラス・パッケージを指定することができるようになりました。

【インターフェースとクラス】

今回のサンプルではjp.glory.cdi.firstパッケージの下に
インターフェイスを配置します。

インターフェイスを実装したクラスは
jp.glory.cdi.first.impl.injectパッケージと
jp.glory.cdi.first.impl.excludeパッケージに配置します。

[インターフェイス]
package jp.glory.cdi.first;

public interface FirstSampleCommand1 {

    void exexute();
}
package jp.glory.cdi.first;

public interface FirstSampleCommand2 {

    void exexute();
}
package jp.glory.cdi.first;

public interface FirstSampleCommand3 {

    void exexute();
}
[実装クラス]
package jp.glory.cdi.first.impl.inject;

import jp.glory.cdi.first.FirstSampleCommand1;

public class FirstSampleComannd1Impl implements FirstSampleCommand1 {

    @Override
    public void exexute() {
        System.out.println("First 001 execute.");
    }
    
}
package jp.glory.cdi.first.impl.inject;

import jp.glory.cdi.first.FirstSampleCommand2;

public class FirstSampleComannd2Impl implements FirstSampleCommand2 {

    @Override
    public void exexute() {
        System.out.println("First 002 execute.");
    }
    
}
package jp.glory.cdi.first.impl.inject;

import jp.glory.cdi.first.FirstSampleCommand3;

public class FirstSampleComannd3Impl implements FirstSampleCommand3 {

    @Override
    public void exexute() {
        System.out.println("First 003 execute.");
    }
    
}
package jp.glory.cdi.first.impl.exclude;

import jp.glory.cdi.first.FirstSampleCommand1;

public class FirstSampleExclude1Impl implements FirstSampleCommand1 {

    @Override
    public void exexute() {
        System.out.println("Exlude 001 execute.");
    }
    
}
package jp.glory.cdi.first.impl.exclude;

import jp.glory.cdi.first.FirstSampleCommand2;

public class FirstSampleExclude2Impl implements FirstSampleCommand2 {

    @Override
    public void exexute() {
        System.out.println("Exlude 002 execute.");
    }
    
}
package jp.glory.cdi.first.impl.exclude;

import jp.glory.cdi.first.FirstSampleCommand3;

public class FirstSampleExclude3Impl implements FirstSampleCommand3 {

    @Override
    public void exexute() {
        System.out.println("Exlude 003 execute.");
    }
    
}

【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_1_1.xsd"
       bean-discovery-mode="all">
    <scan>
        <exclude  name="jp.glory.cdi.first.impl.exclude.*" />
    </scan>
</beans>

beansタグのbean-discovery-mode属性にはallを指定します。

scanタグの下にexludeタグを指定します。
excludeタグのname属性に除外対象のクラス名かパッケージ名を指定します。

".*"をつけた場合は指定したパッケージ直下のクラスが対象になり、
".**"をつけると指定したパッケージと配下にあるパッケージとクラス対象となります。

【動作確認用クラス】

動作確認用にJAX-RSのクラスを作成します。

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import jp.glory.cdi.first.FirstSampleCommand1;
import jp.glory.cdi.first.FirstSampleCommand2;
import jp.glory.cdi.first.FirstSampleCommand3;

@Path("/firstPackage")
public class FristPackageResource {
    
    @Inject
    private FirstSampleCommand1 command1;

    @Inject
    private FirstSampleCommand2 command2;

    @Inject
    private FirstSampleCommand3 command3;

    @GET
    public Response execute() {

        command1.exexute();
        command2.exexute();
        command3.exexute();

        return Response.ok("execute complete.").build();
    }
}

インジェクションするクラスには
Injectアノテーションを付与するだけです。

【実行結果】

First 001 execute.
First 002 execute.
First 003 execute.

実行結果からjp.glory.cdi.first.impl.injectパッケージのクラスが
実行されていることがわかります。