@Inject返回null(NPE)

时间:2016-11-15 23:42:38

标签: jsf cdi

在我的JSF Web应用程序中,在我的客户端转换器类中,即使使用@Inject annotatoin,我指向服务类的变量也返回null(NPE)。

按照转换器类代码:

@FacesConverter(value = "clienteConverter")
public class ClienteConverter implements Converter {

  @Inject
  private ClienteService clienteService;

  @Override
  public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
    if (string == null || string.length() == 0 || string.startsWith("-")) {
      return null;
    }
    try {
      Integer id = Integer.parseInt(string);
      return clienteService.findById(id); // NPE here
    } catch (NumberFormatException | ConverterException e) {
      FacesUtils.error("Erro no converter: " + e.getMessage());
      throw new ConverterException("Erro de conversão do JSF - Valor " + string + " não conversível", e);
    }
  }

我的依赖关系是:

  <dependencies>
    <!-- Usado para geração de PDFs -->
    <dependency>
      <groupId>com.lowagie</groupId>
      <artifactId>itext</artifactId>
      <version>2.1.7</version>
    </dependency>    

    <!-- Usado para geração de arquivos Excel -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.8</version>
      <scope>compile</scope>
    </dependency>    

    <!-- PrimeFaces (biblioteca de componentes) -->
    <dependency>
      <groupId>org.primefaces</groupId>
      <artifactId>primefaces</artifactId>
      <version>5.3</version>
      <scope>compile</scope>
    </dependency>

    <!-- PrimeFaces Themes -->
    <dependency>
      <groupId>org.primefaces.themes</groupId>
      <artifactId>bootstrap</artifactId>
      <version>1.0.10</version>
      <scope>compile</scope>
    </dependency>

    <!-- Mojarra (implementacao do JSF) -->
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.faces</artifactId>
      <version>2.2.9</version>
      <scope>compile</scope>
    </dependency>

    <!-- Weld (implementação do CDI) -->
    <dependency>
      <groupId>org.jboss.weld.servlet</groupId>
      <artifactId>weld-servlet</artifactId>
      <version>2.3.2.Final</version>
      <scope>compile</scope>
    </dependency>

    <!-- Implementacao do Bean Validation -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.2.3.Final</version>
      <scope>compile</scope>
    </dependency>

    <!-- Núcleo do Hibernate -->            
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.1.0.Final</version>
      <scope>compile</scope>
    </dependency>

    <!-- Implementação de EntityManager da JPA -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.1.0.Final</version>
      <scope>compile</scope>
    </dependency>    

    <!-- Driver JDBC do MySQL -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
      <scope>compile</scope>
    </dependency>

    <!-- API de Servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!-- Commons Lang (utilidades) -->
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>    
  </dependencies>

ClienteService.java如下:

package br.com.inovasoftware.erp.web.atbimoveis.service;

import br.com.inovasoftware.erp.web.atbimoveis.model.Cliente;
import br.com.inovasoftware.erp.web.atbimoveis.repository.ClienteRepository;
import br.com.inovasoftware.erp.web.atbimoveis.util.Transacional;
import java.io.Serializable;
import java.util.List;
import javax.inject.Inject;

/**
 *
 * @author Leonardo Rocha
 */
public class ClienteService implements Serializable {
  private static final long serialVersionUID = 1L;

  @Inject
  private ClienteRepository clienteRepository;

  @Transacional
  public void salvar(Cliente cliente) {
    clienteRepository.gravar(cliente);
  }

  @Transacional
  public void excluir(Cliente cliente) {
    clienteRepository.remover(cliente);
  }

  public Cliente findById(Integer id) {
    return clienteRepository.findById(id);
  }

  public List<Cliente> findByNomeOuCpf(String filtro) {
    return clienteRepository.findByNomeOuCpf(filtro);
  }  

  public List<Cliente> findAll() {
    return clienteRepository.findAll();
  }

}

1 个答案:

答案 0 :(得分:1)

使用@Named注释您的ClientService,它将创建一个将注入转换器的bean:

@Named    
public class ClienteService implements Serializable

有关@Named的详细信息,请查看this thread