1

I have such converter:

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

The problem is that entityManager is null. faces-config JSF version is 2.3, Mojarra 2.3.0-m06 is used. In the Application#createConverter() implementation it jumps into the first if's body, but doesn't create the converter. entityManager is not null when the converter is a bean.

Community
  • 1
  • 1
Vladiator
  • 306
  • 3
  • 15

1 Answers1

1

I needed to add a beans.xml file in the WEB-INF directory with the following content:

<?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>
Vladiator
  • 306
  • 3
  • 15
  • Care to explain why you needed to add this? Did you find it mentioned anywhere? – Kukeltje Oct 04 '16 at 06:56
  • I found an example of new features: https://github.com/AnghelLeonard/JSF-2.3/tree/master/JSF23InjectInConverterValidator. The only thing that differed my project from this example was this xml file. Then I found that this file is required in all CDI applications, but i didn't have any problems without it before. – Vladiator Oct 04 '16 at 08:20
  • 1
    @Kukeltje FacesConverter is not a bean defining annotation, so by default it would not be processed by CDI. Native JSF converters don't have support for injecting a persistence context, but CDI beans do (even though PersistenceContext itself is not a CDI injection, CDI beans just happen to integrate with it by default). That's why beans.xml is needed here. – dexter meyers Oct 04 '16 at 12:19
  • 1
    p.s. without beans.xml, the managed=true attribute would be totally ignored. JSF 2.3 will try to locate a converter via CDI, but that would return nothing, then it would try to locate the converter natively, and it would find it. This is a bit of a caveat and is caused by JSF using the same Converter interface and annotation for what are essentially two very different things. – dexter meyers Oct 04 '16 at 12:21