spring boot,没有为依赖项找到类型的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者

时间:2017-03-07 02:59:24

标签: spring-boot spring-data-jpa

@Controller
@EnableAutoConfiguration
public class ControllerShowInfo 
{
    @RequestMapping("/")
    public String rawPage()
    {
        return "rawPage";
    }

    @Autowired
    stockreviewsRepositoryDao repository;

    @RequestMapping("/getBaseInfo")
    @ResponseBody
    public JSONArray getReviewsInfo()
    {
        JSONArray jsonArray = new JSONArray();
        for (stockreviewsBean reviewBean : repository.findAll()) 
        {
            jsonArray.put(reviewBean);
            System.out.println(reviewBean.getTitle());
        }
        return jsonArray;
    }
    public static void main(String[] args) throws Exception 
    {
        SpringApplication.run(ControllerShowInfo.class, args);
    }
}

这是控制器层。

public interface stockreviewsRepositoryDao extends CrudRepository<stockreviewsBean,String> 
{
}
这是道层。 当我运行ControllerShowInfo.class时。有一个问题如下:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为&#39; controllerShowInfo&#39;的错误:通过字段&#39;存储库表达的不满意的依赖关系:没有类型的限定bean [com.yxzh找到依赖项的[.mapper.stockreviewsRepositoryDao] [com.yxzh.mapper.stockreviewsRepositoryDao]:预计至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到[com.yxzh.mapper.stockreviewsRepositoryDao]类型[com.yxzh.mapper.stockreviewsRepositoryDao]类型的限定bean:预计至少有1个bean有资格作为autowire候选者对于这种依赖。依赖注释:

但是当我运行另一个.class

@SpringBootApplication
public class Application 
{
    public static void main(String[] args) throws Exception 
    {
        SpringApplication.run(Application.class, args);
    }
}

并实现CommmandLineRunner

@Component
public class DataInitialization implements CommandLineRunner{

    @Autowired
    stockreviewsRepositoryDao repository;

    @Override
    public void run(String... args) throws Exception 
    {
        System.out.println("-------------------------------");
        int count=0;
        for (stockreviewsBean reviewBean : repository.findAll()) 
        {
            count++;
            System.out.println(reviewBean.getTitle());
        }
        System.out.println(count);
    }
}

效果很好。这让我很困惑。

1 个答案:

答案 0 :(得分:1)

您是否尝试过注释

带有killall node

stockreviewsRepositoryDao

带有@Repository

Application