You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java EE8(EJB3.2)中通过XML配置MDB注入无接口视图Bean

在Java EE 8(EJB 3.2)中通过ejb-jar.xml为MDB注入无接口视图的无状态会话Bean

你需要为MDB配置ejb-local-ref来实现XML方式的注入,因为目标Bean是无接口视图的无状态会话Bean,以下是具体的配置步骤和完整示例:

调整后的MDB代码(移除@EJB注解避免冲突)

@Interceptors(LoggingInterceptor.class)
@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/P76MQ$4bd3dc121fdd4ae5a0730e990a4d14c0"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "MandantenId = '4bd3dc121fdd4ae5a0730e990a4d14c0'"),
        @ActivationConfigProperty(propertyName = "maxMessagesPerSession", propertyValue = "100"),
        @ActivationConfigProperty(propertyName = "maxSessions", propertyValue = "1000") })
public class Protokollierung implements MessageListener {

    private PersonenIndexRepository personenIndexRepository;

    private BearbeitungsFehlerRepository bearbeitungsFehlerRepository;

    private PersonenIndexStandardisierung personenIndexStandardisierung;

    private ErmittlungFachdatenAenderungen ermittlungFachdatenAenderungen;
    private TransaktionsProtokollRepository transaktionsProtokollRepository;

    @Resource
    private String mandantenId;

    public Protokollierung() {

    }

    @PreDestroy
    public void runterfahren() {
        this.schema = null;
        this.mapper = null;
        MDC.clear();
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void onMessage(Message message) {

    }
}

ejb-jar.xml完整配置示例

<ejb-jar 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/ejb-jar_3_2.xsd"
         version="3.2">

    <enterprise-beans>
        <!-- MDB配置 -->
        <message-driven>
            <ejb-name>ProtokollierungMDB</ejb-name>
            <ejb-class>你的实际包路径.Protokollierung</ejb-class>
            <activation-config>
                <activation-config-property>
                    <activation-config-property-name>destinationLookup</activation-config-property-name>
                    <activation-config-property-value>jms/P76MQ$4bd3dc121fdd4ae5a0730e990a4d14c0</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                    <activation-config-property-name>destinationType</activation-config-property-name>
                    <activation-config-property-value>javax.jms.Queue</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                    <activation-config-property-name>acknowledgeMode</activation-config-property-name>
                    <activation-config-property-value>Auto-acknowledge</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                    <activation-config-property-name>messageSelector</activation-config-property-name>
                    <activation-config-property-value>MandantenId = '4bd3dc121fdd4ae5a0730e990a4d14c0'</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                    <activation-config-property-name>maxMessagesPerSession</activation-config-property-name>
                    <activation-config-property-value>100</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                    <activation-config-property-name>maxSessions</activation-config-property-name>
                    <activation-config-property-value>1000</activation-config-property-value>
                </activation-config-property>
            </activation-config>

            <!-- 注入无接口视图Bean:PersonenIndexRepository -->
            <ejb-local-ref>
                <ejb-ref-name>personenIndexRepository</ejb-ref-name>
                <local>你的实际包路径.PersonenIndexRepository</local>
                <ejb-link>PersonenIndexRepositoryBean</ejb-link>
            </ejb-local-ref>

            <!-- 注入无接口视图Bean:BearbeitungsFehlerRepository -->
            <ejb-local-ref>
                <ejb-ref-name>bearbeitungsFehlerRepository</ejb-ref-name>
                <local>你的实际包路径.BearbeitungsFehlerRepository</local>
                <ejb-link>BearbeitungsFehlerRepositoryBean</ejb-link>
            </ejb-local-ref>

            <!-- 注入剩余Bean的配置 -->
            <ejb-local-ref>
                <ejb-ref-name>personenIndexStandardisierung</ejb-ref-name>
                <local>你的实际包路径.PersonenIndexStandardisierung</local>
                <ejb-link>PersonenIndexStandardisierungBean</ejb-link>
            </ejb-local-ref>

            <ejb-local-ref>
                <ejb-ref-name>ermittlungFachdatenAenderungen</ejb-ref-name>
                <local>你的实际包路径.ErmittlungFachdatenAenderungen</local>
                <ejb-link>ErmittlungFachdatenAenderungenBean</ejb-link>
            </ejb-local-ref>

            <ejb-local-ref>
                <ejb-ref-name>transaktionsProtokollRepository</ejb-ref-name>
                <local>你的实际包路径.TransaktionsProtokollRepository</local>
                <ejb-link>TransaktionsProtokollRepositoryBean</ejb-link>
            </ejb-local-ref>
        </message-driven>

        <!-- 目标无状态Bean配置(同包内) -->
        <session>
            <ejb-name>PersonenIndexRepositoryBean</ejb-name>
            <ejb-class>你的实际包路径.PersonenIndexRepository</ejb-class>
            <session-type>Stateless</session-type>
        </session>

        <session>
            <ejb-name>BearbeitungsFehlerRepositoryBean</ejb-name>
            <ejb-class>你的实际包路径.BearbeitungsFehlerRepository</ejb-class>
            <session-type>Stateless</session-type>
        </session>

        <!-- 剩余无状态Bean配置 -->

    </enterprise-beans>
</ejb-jar>

关键配置说明

  • <ejb-ref-name>:必须和MDB中定义的字段名称完全匹配,容器会以此为依据完成注入
  • <local>:指定目标无接口视图Bean的全限定类名
  • <ejb-link>:当目标Bean与MDB在同一个ejb-jar包中时使用,值为目标Bean的<ejb-name>,用于明确关联目标Bean;若目标Bean不在同包内,可移除该标签,容器会通过JNDI查找<local>指定的类匹配目标Bean

内容的提问来源于stack exchange,提问作者André

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:16:24