我在运行项目时收到以下错误消息
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'tipoEstadoCivilController'的bean时出错:通过字段'tipoEstadoCivilService'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'tipoEstadoCivilService'的bean时出错:通过字段'tipoEstadoCivilRepository'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'tipoEstadoCivilRepository'的bean时出错:init方法的调用失败;嵌套异常是org.springframework.data.mapping.PropertyReferenceException:找不到类型为TipoEstadoCivil的属性filtrar!
不幸的是我找不到什么错误。
以下是与错误相关的主要类和接口
类TipoEstadoCivil
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Vector2D implements Iterator<Integer> {
private List<List<Integer>> vec2d;
private Iterator<Integer> it;
private int counter = 0;
private int size;
public Vector2D(List<List<Integer>> vec2d) {
this.vec2d = new ArrayList<List<Integer>>();
if(vec2d.size() == 0) {
this.it = null;
} else {
this.size = vec2d.size();
System.out.println("this size is " + size); // * returns 3
this.it = vec2d.get(0).iterator();
}
}
@Override
public Integer next() {
System.out.println("what happens here? " + vec2d.size()); **// * why it returns 0?? should it return 3??**
if(hasNext()) {
return it.next();
}
return -1;
}
@Override
public boolean hasNext() {
if(it == null) {
return false;
}
while(counter < size) {
if(it.hasNext()) {
return true;
}
counter++;
if(counter >= size) {
return false;
}
System.out.println("counter is " + counter + " size is " + vec2d.size());
List<Integer> l = vec2d.get(counter);
it = l.iterator();
}
return false;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
List<Integer> list3 = new ArrayList<Integer>();
list.add(1);
list.add(2);
list2.add(3);
list3.add(4);
list3.add(5);
list3.add(6);
List<List<Integer>> lists = new ArrayList<List<Integer>>();
lists.add(list);
lists.add(list2);
lists.add(list3);
Vector2D vec = new Vector2D(lists);
System.out.println(vec.next());
}
}
接口TipoEstadoCivilRepository
package sgman.model;
import org.hibernate.validator.constraints.NotBlank;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;
@Entity
@Table(name = "tipo_estado_civil")
public class TipoEstadoCivil implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_tipo_estado_civil", updatable = false, nullable = false)
private Long idTipoEstadoCivil;
@Column(name = "nome_estado_civil", nullable = false)
@NotBlank
@Size(min = 3, max = 32)
private String nomeEstadoCivil;
Getter and Setter
Equals and hashCode
}
接口TipoEstadoCivilQueries
package sgman.repository;
import sgman.model.TipoEstadoCivil;
import sgman.repository.helper.TipoEstadoCivilQueries;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface TipoEstadoCivilRepository extends JpaRepository<TipoEstadoCivil, Long>, TipoEstadoCivilQueries {
Optional<TipoEstadoCivil> findByNomeEstadoCivilIgnoreCase(String nomeEstadoCivil);
}
类TipoEstadoCivilImpl
package sgman.repository.helper;
import sgman.model.TipoEstadoCivil;
import sgman.repository.filter.TipoEstadoCivilFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface TipoEstadoCivilQueries {
Page<TipoEstadoCivil> filtrar(TipoEstadoCivilFilter filtro, Pageable pageable);
}
Class TipoEstadoCivilController
package sgman.repository.helper;
import sgman.repository.filter.TipoEstadoCivilFilter;
import sgman.model.TipoEstadoCivil;
import sgman.repository.paginacao.PaginacaoUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class TipoEstadoCivilImpl implements TipoEstadoCivilQueries {
@PersistenceContext
private EntityManager manager;
@Autowired
private PaginacaoUtil paginacaoUtil;
private void addFilter(TipoEstadoCivilFilter tipoEstadoCivilFilter, Criteria criteria) {
if (tipoEstadoCivilFilter != null) {
if (!StringUtils.isEmpty(tipoEstadoCivilFilter.getNomeEstadoCivil())) {
criteria.add(Restrictions.ilike("nomeEstadoCivil", tipoEstadoCivilFilter.getNomeEstadoCivil(), MatchMode.ANYWHERE));
}
}
}
private Long count(TipoEstadoCivilFilter tipoEstadoCivilFilter) {
Criteria criteria = manager.unwrap(Session.class).createCriteria(TipoEstadoCivil.class);
addFilter(tipoEstadoCivilFilter, criteria);
criteria.setProjection(Projections.rowCount());
return (Long) criteria.uniqueResult();
}
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public Page<TipoEstadoCivil> filtrar(TipoEstadoCivilFilter tipoEstadoCivilFilter, Pageable pageable) {
Criteria criteria = manager.unwrap(Session.class).createCriteria(TipoEstadoCivil.class);
paginacaoUtil.preparar(criteria, pageable);
addFilter(tipoEstadoCivilFilter, criteria);
return new PageImpl<>(criteria.list(), pageable, count(tipoEstadoCivilFilter));
}
}
错误消息
package sgman.controller;
import sgman.controller.page.PageWrapper;
import sgman.exception.TipoEstadoCivilAlreadyExists;
import sgman.model.TipoEstadoCivil;
import sgman.repository.TipoEstadoCivilRepository;
import sgman.repository.filter.TipoEstadoCivilFilter;
import sgman.service.TipoEstadoCivilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
@Controller
@RequestMapping("/tipoEstadoCivil")
public class TipoEstadoCivilController {
@Autowired
private TipoEstadoCivilService tipoEstadoCivilService;
@Autowired
private TipoEstadoCivilRepository tipoEstadoCivilRepository;
@RequestMapping("/novo")
public ModelAndView novo(TipoEstadoCivil tipoEstadoCivil) {
return new ModelAndView("tipoEstadoCivil/novoEstadoCivil");
}
@RequestMapping(value = "/novo", method = RequestMethod.POST)
public ModelAndView cadastrar(@Valid TipoEstadoCivil tipoEstadoCivil, BindingResult result, RedirectAttributes attributes) {
if (result.hasErrors()) {
return novo(tipoEstadoCivil);
}
try {
tipoEstadoCivilService.saveTipoEstadoCivil(tipoEstadoCivil);
} catch (TipoEstadoCivilAlreadyExists e) {
result.rejectValue("nomeEstadoCivil", e.getMessage(), e.getMessage());
return novo(tipoEstadoCivil);
}
attributes.addFlashAttribute("mensagem", "Estado civil salvo com sucesso");
return new ModelAndView("redirect:/tipoEstadoCivil/novo");
}
@RequestMapping(method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody ResponseEntity<?> salvar(@RequestBody @Valid TipoEstadoCivil tipoEstadoCivil, BindingResult result) {
if (result.hasErrors()) {
return ResponseEntity.badRequest().body(result.getFieldError("nomeEstadoCivil").getDefaultMessage());
}
tipoEstadoCivil = tipoEstadoCivilService.saveTipoEstadoCivil(tipoEstadoCivil);
return ResponseEntity.ok(tipoEstadoCivil);
}
@GetMapping
public ModelAndView pesquisar(TipoEstadoCivilFilter estiloFilter, BindingResult result, @PageableDefault(size = 2) Pageable pageable, HttpServletRequest httpServletRequest) {
ModelAndView mv = new ModelAndView("estilo/PesquisaEstilos");
PageWrapper<TipoEstadoCivil> paginaWrapper = new PageWrapper<>(tipoEstadoCivilRepository.filtrar(estiloFilter, pageable), httpServletRequest);
mv.addObject("pagina", paginaWrapper);
return mv;
}
}
欢迎任何帮助尝试。 最好的祝福。
答案 0 :(得分:0)
类TipoEstadoCivilService
package sgman.service;
import sgman.exception.TipoEstadoCivilAlreadyExists;
import sgman.model.TipoEstadoCivil;
import sgman.repository.TipoEstadoCivilRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.Optional;
@Service
public class TipoEstadoCivilService {
@Autowired
private TipoEstadoCivilRepository tipoEstadoCivilRepository;
@Transactional
public TipoEstadoCivil saveTipoEstadoCivil(TipoEstadoCivil tipoEstadoCivil) {
Optional<TipoEstadoCivil> tipoEstadoCivilOptional = tipoEstadoCivilRepository.findByNomeEstadoCivilIgnoreCase(tipoEstadoCivil.getNomeEstadoCivil());
if (tipoEstadoCivilOptional.isPresent()) {
throw new TipoEstadoCivilAlreadyExists("Estado civil já cadastrado");
}
return tipoEstadoCivilRepository.saveAndFlush(tipoEstadoCivil);
}
}