現在、SpringBootを利用してWebアプリケーションを作っています。
Controllerクラスで異なるパッケージにあるクラスを@Autowiredでフィールドに定義したところ、
SpringBootの起動時にエラーが発生しました。
Contorollerクラスと同一パッケージの時には発生しません。
Controllerクラスで異なるパッケージにあるクラスを
@Autowiredでフィールドに定義するにはどうすればよいでしょうか?
分かる方がいらっしゃましたらご教授お願い致します。
Controllerクラス
lang
package jp.spring.ysk.app.controller; import java.util.List; import jp.spring.ysk.entity.Glossary; import jp.spring.ysk.entity.IYskRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { // ここの@Autowiredでエラーが発生 @Autowired IYskRepository repository; @RequestMapping("/") public String index(Model model){ model.addAttribute("msg", "サイドバーのメッセージ"); List<Glossary> list = repository.findAll(); model.addAttribute("glossaryList", list); return "index"; } }
@Autowired対象のクラス
lang
package jp.spring.ysk.entity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; public interface IYskRepository extends JpaRepository<Glossary, Long>{ }
エラー内容
lang
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: jp.spring.ysk.entity.IYskRepository jp.spring.ysk.app.controller.HelloController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [jp.spring.ysk.entity.IYskRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
まだ回答がついていません
会員登録して回答してみよう