请解释CDI中@Produces注解 [英] Please explain the @Produces annotation in CDI

查看:16
本文介绍了请解释CDI中@Produces注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 CDI 中的 @Produces 注释,但我不明白它的用法.

I have read about the @Produces annotation in CDI, but I don't understand its usage.

public class Resources {

// Expose an entity manager using the resource producer pattern
@SuppressWarnings("unused")
@PersistenceContext
@Produces
private EntityManager em;                                        // 

@Produces
Logger getLogger(InjectionPoint ip) {                            // 
    String category = ip.getMember()
                        .getDeclaringClass()
                        .getName();
    return Logger.getLogger(category);
}

@Produces
FacesContext getFacesContext() {                                 // 
    return FacesContext.getCurrentInstance();
}

}

取自:http://www.jboss.org/jdf/quickstarts/jboss-as-quickstart/guide/GreeterQuickstart/#GreeterQuickstart-

容器如何知道调用生产者方法?如果我注入一个EntityManager,容器如何调用@produces EntityManager?以及如何调用 getLogger 生产者方法?

How does the container know to call a producer method? If I inject an EntityManager, how does the container call the @produces EntityManager? And how would a getLogger producer method get called?

我也看不出有什么理由要经历所有的麻烦.

I also don't see the reason to go through all of the trouble.

推荐答案

CDI 规范@Produces注解:

生产者方法充当要注入的对象的来源,其中:

• 被注入的对象不需要是bean 的实例,或者
• 要注入的对象的具体类型可能会在运行时发生变化,或者
• 对象需要一些不由 bean 构造函数执行的自定义初始化.

A producer method acts as a source of objects to be injected, where:

• the objects to be injected are not required to be instances of beans, or
• the concrete type of the objects to be injected may vary at runtime, or
• the objects require some custom initialization that is not performed by the bean constructor.

例如,假设您想在 Java EE 托管组件(如实体管理器)和其他 CDI 组件之间架起桥梁,您可以使用 @Produces 批注.另一个好处是您不必在整个数据域层中重复 @PersistenceContext 注释.

Let's say, for example, that you wanted to bridge between a Java EE managed component like an entity manager and other CDI components, you could utilize the @Produces annotation. Another benefit being that you avoid having to duplicate @PersistenceContext annotations throughout your data domain layer.

class A {
    @PersistenceContext       // This is a JPA annotation
    @Produces                 // This is a CDI 'hook'
    private EntityManager em; 
}

class B {
   @Inject                    // Now we can inject an entity manager
   private EntityManager em;
}

另一个方便的用途是绕过没有 CDI 友好 bean(例如,没有默认构造函数)的库:

Another handy use is for getting around libraries that do not have CDI friendly beans (for example, no default constructors):

class SomeTPLClass {
    public SomeTPLClass(String id) {
    }
}

class SomeTPLClassProducer {
    @Produces
    public SomeTPLClass getInstance() {
        return new SomeTPLClass("");
    }
}

用于生产的 Javadoc 还展示了一个有趣的(但相当罕见的情况),即生成一个命名集合,该集合稍后可以注入其他托管 bean(非常酷):

The Javadoc for produces also shows an interesting (but fairly rare case) of producing a named collection that can later on be injected into other managed beans (very cool):

public class Shop { 
    @Produces @ApplicationScoped 
    @Catalog @Named("catalog") 
    private List<Product> products = new LinkedList<Product>(8);

    //...
}

public class OrderProcessor {
    @Inject
    @Catalog
    private List<Product> products;
}

容器负责处理所有标有@Produces 注释的方法和字段,并且通常会在您的应用程序部署时执行此操作.然后,根据需要,处理过的方法和字段将用作托管 bean 注入点解析的一部分.

The container is responsible for processing all methods and fields marked with a @Produces annotation, and will normally do this when your application is deployed. The processed methods and fields will then be used as part of the injection point resolution for managed beans, as needed.

这篇关于请解释CDI中@Produces注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆