現在、SpringBootを利用してWebアプリケーションを作っています。
Controllerクラスで異なるパッケージにあるクラスを@Autowiredでフィールドに定義したところ、
SpringBootの起動時にエラーが発生しました。
Contorollerクラスと同一パッケージの時には発生しません。
Controllerクラスで異なるパッケージにあるクラスを
@Autowiredでフィールドに定義するにはどうすればよいでしょうか?
分かる方がいらっしゃましたらご教授お願い致します。
Controllerクラス
lang
1package jp.spring.ysk.app.controller; 2 3import java.util.List; 4import jp.spring.ysk.entity.Glossary; 5import jp.spring.ysk.entity.IYskRepository; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.web.bind.annotation.RequestMapping; 10 11@Controller 12public class HelloController { 13 14 // ここの@Autowiredでエラーが発生 15 @Autowired 16 IYskRepository repository; 17 18 @RequestMapping("/") 19 public String index(Model model){ 20 model.addAttribute("msg", "サイドバーのメッセージ"); 21 22 List<Glossary> list = repository.findAll(); 23 model.addAttribute("glossaryList", list); 24 return "index"; 25 } 26}
@Autowired対象のクラス
lang
1package jp.spring.ysk.entity; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4import org.springframework.stereotype.Repository; 5public interface IYskRepository extends JpaRepository<Glossary, Long>{ 6}
エラー内容
lang
1org.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)}

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。