私のJSF Webアプリケーションでは、クライアントのコンバータークラスで、サービスクラスを指す変数@Inject annotatoinを使用してもnull(NPE)が返されます。@Inject returns 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();
}
}
あなたのクラスを見ると、それらを[bean定義アノテーション](http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#bean_defining_annotations)で注釈しているわけではありません。したがって、問題は - beans.xmlを持っていますか?あなたの[bean dicovery mode](http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#bean_archive)とは何ですか?クラスはBeanとして処理されますか? – Siliarus