2016-10-02 30 views
1

我有這樣的轉換器:鑽嘴魚科2.3.0-M06,@PersistenceContext字段爲空

@FacesConverter(managed = true, value = "myConverter") 
public class MyConverter implements Converter { 
    @PersistenceContext(unitName = MyService.PERSISTENCE_NAME) 
    private EntityManager entityManager; 
    ... 
} 

的問題是,的EntityManager爲空。 faces-config JSF版本是2.3,使用Mojarra 2.3.0-m06。在Application#createConverter() implementation它跳轉到第一個if的主體,但不創建轉換器。 entityManager當爲the converter is a bean時不爲空。

回答

1

我需要添加一個beans.xml的文件中的WEB-INF目錄包含以下內容:

<?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> 
+0

護理解釋爲什麼你需要添加呢?你有沒有在任何地方發現它? – Kukeltje

+0

我發現了一個新功能的例子:https://github.com/AnghelLeonard/JSF-2.3/tree/master/JSF23InjectInConverterValidator。這個例子中唯一不同於我的項目的是這個xml文件。然後我發現這個文件在所有CDI應用程序中都是必需的,但是沒有它之前我沒有任何問題。 – Vladiator

+1

@Kukeltje FacesConverter不是定義註釋的bean,所以默認情況下它不會被CDI處理。原生JSF轉換器不支持注入持久化上下文,但是CDI bean(儘管PersistenceContext本身不是CDI注入,CDI bean恰好在默認情況下與它集成)。這就是爲什麼beans.xml需要在這裏。 –

相關問題