Is @Dependent scope not default in Wildfly?

隐身守侯 提交于 2019-12-01 14:47:30

When using CDI in Java EE 7 (CDI 1.1), the default bean discovery mode is annotated. Which means that any bean with an explicitly specified scope is available for injection.

So to make your bean B available for injection, you can either:

  1. Declare an explicit scope on class B (that's what you are doing when putting @Dependent)
  2. Declare a beans.xml file with the bean-discovery-mode attribute set to all. This will make all beans of your archive available for injection (same behavior than Java EE 6 (CDI 1.0)).

The beans.xml file must be in the META-INF folder and looks like:

<?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"
       version="1.1" 
       bean-discovery-mode="all">

</beans>

However, i would not recommend using bean-discovery-mode="all".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!