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

CDI 1.0与1.2中beanManager.getBeans的行为差异及适配修改咨询

CDI 1.0 vs 1.2: Differences in beanManager.getBeans() Behavior & Fixes for Missing Results

Great question — this is a common gotcha when upgrading from CDI 1.0 to 1.2, rooted in stricter adherence to the CDI specification around annotation matching. Let's break this down:

Key Behavior Difference

In CDI 1.0, the container used a relatively loose matching strategy for AnnotationLiteral instances — as long as the annotation type and explicit property values matched, it would return the corresponding beans.

CDI 1.2 tightened this up to strictly follow the specification's rules for annotation equality:

  • The container now relies on proper implementation of equals() and hashCode() for AnnotationLiteral subclasses.
  • It requires all annotation properties (including those with default values) to match exactly between the AnnotationLiteral and the qualifier on your bean.

Why Your Call Fails in CDI 1.2

Most likely, your AnnotationLiteral subclass either:

  1. Doesn't properly override equals() and hashCode() to account for your annotation's properties, or
  2. Doesn't explicitly implement all properties of your custom qualifier (including default values), leading to a mismatch.

Fixes to Get Results Again

1. Correctly Implement AnnotationLiteral with equals() and hashCode()

While the base AnnotationLiteral class provides a default implementation, it's often not sufficient for qualifiers with custom properties. You need to ensure your subclass accurately reflects all properties of your qualifier so the container can match it.

Example for a qualifier @MyQualifier(value = "test"):

import javax.enterprise.util.AnnotationLiteral;
import java.util.Objects;

public class MyQualifierLiteral extends AnnotationLiteral<MyQualifier> implements MyQualifier {
    private final String value;

    public MyQualifierLiteral(String value) {
        this.value = value;
    }

    @Override
    public String value() {
        return value;
    }

    // Override equals/hashCode to ensure precise matching
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyQualifierLiteral that = (MyQualifierLiteral) o;
        return Objects.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }
}

2. Match All Annotation Properties (Including Defaults)

If your qualifier has properties with default values (e.g., boolean enabled() default true;), your AnnotationLiteral must explicitly return those default values. CDI 1.2 won't assume default values — it expects the literal to mirror the exact annotation on the bean.

For example, if your bean uses @MyQualifier (relying on the default enabled=true), your literal must include:

@Override
public boolean enabled() {
    return true;
}

3. Simplify with Annotation Type Matching (If Possible)

If your qualifier doesn't have custom properties, or you only need to match by qualifier type, skip the AnnotationLiteral entirely and pass the class directly:

beanManager.getBeans(Abc.class, MyQualifier.class);

This is more reliable in CDI 1.2 and avoids literal-related matching issues.

4. Verify Bean Qualifiers

Double-check that your target bean is annotated with exactly the qualifiers you're trying to match. If the bean has additional qualifiers that you haven't included in your getBeans() call, CDI 1.2 won't return it (since getBeans() requires all specified qualifiers to be present on the bean).


内容的提问来源于stack exchange,提问作者Sudhish Nair

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:37:19