构造函数注入不适用于@Statless bean中的限定符

时间:2018-03-20 12:24:00

标签: java weblogic weblogic12c java-ee-7

我遇到了下一个令人兴奋的问题。如果我尝试在Weblogic 12.2.1.3.0上运行下一个代码:

package hu.sample.bean;

import lombok.extern.slf4j.Slf4j;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.Random;

@Slf4j
@Stateless
public class Salutatory {

    private Butler butler;
    private Butler assistant;

    public Salutatory() {}

    @Inject
    public Salutatory(@MainButler Butler mainButler,
                      @Assistant Butler assistant) {
        this.butler = mainButler;
        this.assistant = assistant;
        log.debug("Call with {} {}", mainButler, assistant);
    }

    public String salute(String name) {
        if (new Random().nextBoolean()) {
            return butler.welcome(name + "1");
        } else  {
            return assistant.welcome(name + "2");
        }
    }
}
package hu.sample.bean;

public interface Butler {

    String welcome(String name);
}
package hu.sample.bean;

import javax.inject.Qualifier;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface MainButler {
}
package hu.sample.bean;

import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Assistant {
}
package hu.sample.bean;

import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Any;
import javax.inject.Named;

@Stateless
@MainButler
public class George implements Butler {

    @Override
    public String welcome(String name) {
        return String.format("Hello, I'm Geroge, I'm the main butler. Welcom here %s! (%s)", name, this.getClass());
    }
}
package hu.sample.bean;

import javax.ejb.Stateless;

@Stateless
@Assistant
public class Peter implements Butler {

    @Override
    public String welcome(String name) {
        return String.format("Hello, I'm Peter, I'm an assistant. Welcom here %s! (%s)", name, this.getClass());
    }
}

我得到了下一个例外。:

weblogic.management.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type Butler with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] public hu.sample.bean.Salutatory_mv77ws_Impl(Butler, Butler)
  at hu.sample.bean.Salutatory_mv77ws_Impl.<init>(Salutatory_mv77ws_Impl.java:0)
WELD-001475: The following beans match by type, but none have matching qualifiers:
  - Session bean [class hu.sample.bean.Peter with qualifiers [@Assistant @Any]; local interfaces are [Butler],
  - Session bean [class hu.sample.bean.George with qualifiers [@MainButler @Any]; local interfaces are [Butler]

好的,我理解这一点,并创建了Butler的默认实现。但在这种情况下,CDI每次都会注入默认实现,忽略了限定符。为什么呢?

如果我使用二传手注射,场注射等,那么限定符效果很好......

如果Salutatory是@WebServlet,带有限定符的构造函数注入也很有效。

但是,为什么没有使用限定符的构造函数注入在@Stateless bean中运行良好?

非常感谢你的回答!

1 个答案:

答案 0 :(得分:0)

要注入EJB,最好在EJB注入上下文中使用javax.ejb.EJB批注。 (即注入点本身就是EJB)

在这种情况下,您可以使用类的简单名称作为beanName来选择要注入的实现。

对于您而言,不要在构造函数中限定Bean,而是对每个字段进行限定。


    @EJB(beanName="George")
    private Butler butler;

    @EJB(beanName="Peter")
    private Butler assistant;