Olá pessoal,
Estou aqui tentando entender porque consigo utilizar o CDI chamando meu bean em uma página xhtml, e chamando um serviço anotando o atributo com @Inject dentro do bean chamado na página já não tenho o mesmo sucesso e o mesmo retorna nulo.
Abaixo a classe de teste. Até o XHTML chama com sucesso utilizando o EL. Ou seja o @ManaedBean está funcionando.
package br.com.app.controller;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
@ManagedBean
public class Soma implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
public static Somador somador;
@PostConstruct
public void init(){
System.out.println(" Bean executado! ");
somador.soma(2);
}
public int retorna() {
System.out.println("Estou tentando usar o que deveria ser injetado aqui...");
System.out.println(somador.soma(2)); //neste momento retorna nulo
return 1;
}
}
Aqui o que estou tentando injetar no código acima:
package br.com.app.controller;
import java.io.Serializable;
import javax.inject.Inject;
import javax.inject.Named;
@Named
public class Somador implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
public int soma(int i) {
return i++;
}
}
Meu beans.xml localizado em src/main/resources
<?xml version="1.0" encoding="UTF-8" ?>
Meu web.xml:
<?xml version="1.0" encoding="UTF-8"?>
estudos
index.xhtml
Faces Servlet
javax.faces.webapp.FacesServlet
1
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>
javax.enterprise.inject.spi.BeanManager
</resource-env-ref-type>
</resource-env-ref>