Eclipseにて、「javaアプリケーション」より実行を行なったファイルに、エラーが発生してしまいました。
以下は、MyDataDaoImpl.javaになります。
package com.tuyano.springboot; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; public class MyDataDaoImpl implements MyDataDao<MyData> { private static final long serialVersionUID = 1L; @PersistenceContext private EntityManager entityManager; public MyDataDaoImpl() { super(); } public MyDataDaoImpl(EntityManager manager) { entityManager = manager; } @Override public List<MyData> getAll(){ List<MyData> list = null; CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<MyData> query = builder.createQuery(MyData.class); Root<MyData> root = query.from(MyData.class); query.select(root); list = (List<MyData>)entityManager.createQuery(query).getResultList(); return list; } @Override public MyData findById(long id) { return (MyData)entityManager.createQuery("from MyData where id = " + id).getSingleResult(); } @Override public List<MyData> findByName(String name) { return (List<MyData>)entityManager.createQuery("from MyData where nama = " + name).getResultList(); } }
以下は、HeloController.javaになります。
package com.tuyano.springboot; import java.util.Optional; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.tuyano.springboot.repositories.MyDataRepository; @Controller public class HeloController { @Autowired MyDataRepository repository; @Autowired MyDataDaoImpl dao; @RequestMapping(value = "/",method = RequestMethod.GET) public ModelAndView index(@ModelAttribute("formModel") MyData mydata,ModelAndView mav) { mav.setViewName("index"); mav.addObject("msg","MyDataのサンプルです。"); Iterable<MyData> list = dao.getAll(); mav.addObject("datalist",list); return mav; } @RequestMapping(value = "/",method = RequestMethod.POST) @Transactional(readOnly=false) public ModelAndView form(@ModelAttribute("formModel")@Validated MyData mydata, BindingResult result,ModelAndView mov) { ModelAndView res = null; if(!result.hasErrors()) { repository.saveAndFlush(mydata); res = new ModelAndView("redirect:/"); }else { mov.setViewName("index"); mov.addObject("msg","sorry,error is occured..."); Iterable<MyData> list = repository.findAll(); mov.addObject("datalist",list); res = mov; } return res; } @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) public ModelAndView edit(@ModelAttribute MyData mydata, @PathVariable int id, ModelAndView mav) { mav.setViewName("edit"); mav.addObject("title", "edit mydata."); Optional<MyData> data = repository.findById((long)id); mav.addObject("formModel",data.get()); System.out.println("ok"); return mav; } @RequestMapping(value="/edit",method=RequestMethod.POST) @Transactional(readOnly=false) public ModelAndView update(@ModelAttribute MyData mydata,ModelAndView mav){ repository.saveAndFlush(mydata); System.out.println(mydata); return new ModelAndView("redirect:/"); } @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET) public ModelAndView delete(@PathVariable int id,ModelAndView mav) { mav.setViewName("delete"); mav.addObject("title","delete mydata."); Optional<MyData> data = repository.findById((long)id); System.out.println(data.get()); mav.addObject("formModel",data.get()); return mav; } @RequestMapping(value = "/delete",method = RequestMethod.POST) public ModelAndView remove(@RequestParam long id,ModelAndView mav) { repository.deleteById(id); return new ModelAndView("redirect:/"); } @PostConstruct public void init() { MyData d1 = new MyData(); d1.setName("tuyano"); d1.setAge(123); d1.setMail("syoda@tuyano.com"); d1.setMemo("a"); repository.saveAndFlush(d1); MyData d2 = new MyData(); d2.setName("hanako"); d2.setAge(15); d2.setMail("hanako@flower"); d2.setMemo("b"); repository.saveAndFlush(d2); MyData d3 = new MyData(); d3.setName("sachiko"); d3.setAge(37); d3.setMail("sachico@happy"); d3.setMemo("c"); repository.saveAndFlush(d3); } } class DataObject{ private int id; private String name; private String value; public DataObject(int id, String name, String value){ super(); this.id = id; this.name = name; this.value = value; } public int getId(){ return id;} public void setId(int id){ this.id = id;} public String getName() { return name;} public void setName(String name){ this.name = name; } public String getValue(){ return value; } public void setValue(String value){ this.value = value; } }
以下はMyData.javaです
package com.tuyano.springboot; import java.io.Serializable; import java.util.List; public interface MyDataDao<T> extends Serializable { public List<T> getAll(); public T findById(long id); public List<T> findByName(String name); }
以下はエラー内容となります。
*************************** APPLICATION FAILED TO START *************************** Description: Field dao in com.tuyano.springboot.HeloController required a bean of type 'com.tuyano.springboot.MyDataDaoImpl' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.tuyano.springboot.MyDataDaoImpl' in your configuration.
エラー内容で検索し調べたのですが、解決できませんでした。
こちらのエラーにつきまして、ご助言頂ければ幸いです!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/23 08:43 編集
2020/05/23 08:43