Frage

I recently migrated a simple Java EE 6 project to Java EE 7. In detail this means I just changed the dependency from javax:javaee-api:6.0 to javax:javaee-api:7.0 and deployed it to Glassfish 4 instead of Glassfish 3.

Afterwards the application did not work anymore, because CDI could not inject the annoted dependencies.

War es hilfreich?

Lösung

The problem was, that I did not know that the deployment descriptor and also the default behaviour to find dependencies has changed in Java EE 7.

The new deployment descriptor (beans.xml) has to look like this:

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

Important is the attribute bean-discovery-mode, which configures how CDI finds all the dependencies. With setting it to bean-discovery-mode="all", CDI behaves like in Java EE 6.

Andere Tipps

Setting bean-discovery-mode="all" works, but could also be set to "annotated" depending on how your beans are set up.

Or you could eliminate beans.xml altogether to create an implicit archive. See the Java EE 7 Tutorial section on packaging CDI applications. To use implicit archives, you need a scope-defining annotation on your beans.

To get rid of the beans.xml file and make your injections work on Glassfish 4, you have to change the packages of the scopes as well:

javax.faces.bean.ApplicationScoped -> javax.enterprise.context.ApplicationScoped
javax.faces.bean.RequestScoped -> javax.enterprise.context.RequestScoped
javax.faces.bean.SessionScoped -> javax.enterprise.context.SessionScoped
javax.faces.bean.ViewScoped -> javax.faces.view.ViewScoped
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top