简体   繁体   English

在 CDI 实现项目中包含空 beans.xml 的目的是什么?

[英]what is the purpose of including empty beans.xml in CDI implemenations projects?

I am using weld ,a RI of CDI as dependency injection component in my JSF-EJB-JPA web app.我在我的 JSF-EJB-JPA Web 应用程序中使用weld ,CDI 的一个 RI 作为依赖项注入组件。 I see in my project we have empty beans.xml in META-INF/beans.xml in ejb.jar and WEB-INF/beans.xml in my WAR.我在我的项目中看到ejb.jar中的META-INF/beans.xml和 WAR 中的WEB-INF/beans.xml中有空的 beans.xml。 I don't get it why we need to keep empty beans.xml when there nothing defined in that file?我不明白为什么我们需要在该文件中没有定义任何内容时保留空的beans.xml

CDI needs to scan all the classes of a bean archive at start-up and fire a bunch of events because almost any class is automatically a managed bean (read more here ), even if it doesn't have any annotations. CDI 需要在启动时扫描 bean 存档的所有类并触发一系列事件,因为几乎所有类都会自动成为托管 bean( 在此处阅读更多内容),即使它没有任何注释。

This would incur quite a bit of overhead, especially for jar files that are not meant to have any beans, and it is therefore beneficial to explicitly indicate which bean archives should be scanned by including the beans.xml .这会产生相当多的开销,尤其是对于不打算包含任何 bean 的 jar 文件,因此通过包含beans.xml来明确指示应扫描哪些 bean 档案是有益的。

1 1

A completely empty beans.xml is the same as having a beans.xml inside the archive with the following content:一个完全空的beans.xml与在存档中有一个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">
</beans>

Because of bean-discovery-mode="all" the archive will be scanned for beans.由于bean-discovery-mode="all"将扫描存档以查找 bean。 No need to annotate them.无需注释它们。

2 2

A non-existent beans.xml it is the same as having a beans.xml inside the archive with the following content:一个不存在的beans.xml它与在存档中具有以下内容的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="annotated">

</beans>

Because of bean-discovery-mode="annotated" the archive will be scanned for beans among classes that are annotated (eg @Dependent ).由于bean-discovery-mode="annotated"存档将在被注释的类中扫描 bean(例如@Dependent )。 All other classes will be ignored, therefore cannot be injected as beans.所有其他类将被忽略,因此不能作为 bean 注入。

3 3

A third option is to declare bean-discovery-mode="none" in which case the server never scans the archive for beans.第三个选项是声明bean-discovery-mode="none"在这种情况下服务器从不扫描 bean 的存档。

4 4

Now for the case on which you want to load a class as a bean but you cannot access the archive (eg external library) and the class is not annotated, the solution is to use a Producer methods (with or without qualifiers).现在对于您想将类作为 bean 加载但无法访问存档(例如外部库)并且类没有注释的情况,解决方案是使用Producer 方法(带或不带限定符)。

It is used in certain limited situations它在某些有限的情况下使用

http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html

Some CDI features like decorators would be declared in this file一些像装饰器这样的 CDI 特性会在这个文件中声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM