(Spring Boot)助けてください!DaoがAutowiredされない、もう嫌だ。
Spring Bootで簡単なRestControllerを作ろうとしてます。
その際データベースアクセスは、ORマッパーとしてDoma2を使うことにしました。
これがまたうまくいかないことといったらありません。
うまくいかなかったところは、DaoをAutwiredができなかったところです。
簡単なサービスクラスが下記です。
@Service public class DomaServiceImpl implements DomaService { @Autowired private DomaDao dao; public List<DomaEntity> selectAll() { List<DomaEntity> list = dao.selectAll(); return list; } }
これでサーバを起動すると下記がコンソールに出力されます。
*************************** APPLICATION FAILED TO START *************************** Description: Field dao in com.springboot.service.DomaServiceImpl required a bean of type 'com.springboot.dao.DomaDao' 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.springboot.dao.DomaDao' in your configuration.
もう調べるところはないんじゃないかというくらいググりまくりました。
要はコンポーネントスキャンだ、と思って下記も試してみたら、
@SpringBootApplication(scanBasePackages={"com.springboot.dao"}) public class Doma2Application { public static void main(String[] args) { SpringApplication.run(Doma2Application.class, args); } }
サーバが起動はしたものの、なぜかコントローラがリクエストを受け付けなくなりました。
もうギブ。。。
どなたか指摘願えませんでしょうか。
@SpringBootApplication public class Doma2Application { public static void main(String[] args) { SpringApplication.run(Doma2Application.class, args); } }
@RestController @ResponseBody public class DomaController { @Autowired private DomaService service; @GetMapping("/selectall") public List<DomaEntity> selectAll() { System.out.println("selectAll"); List<DomaEntity> list = service.selectAll(); return list; } }
@ConfigAutowireable @Dao public interface DomaDao { @Select public List<DomaEntity> selectAll(); }
import lombok.Data; @Entity @Data public class DomaEntity { Integer id; String name; }
@Service public interface DomaService { public List<DomaEntity> selectAll(); }
@Service public class DomaServiceImpl implements DomaService { @Autowired private DomaDao dao; public List<DomaEntity> selectAll() { List<DomaEntity> list = dao.selectAll(); return list; } }