0
votes
@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);
    }
}

this is controller layer.

public interface stockreviewsRepositoryDao extends CrudRepository<stockreviewsBean,String> 
{
}

this is Dao layer. when I run ControllerShowInfo.class. There is a problem as follows:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controllerShowInfo': Unsatisfied dependency expressed through field 'repository': No qualifying bean of type [com.yxzh.mapper.stockreviewsRepositoryDao] found for dependency [com.yxzh.mapper.stockreviewsRepositoryDao]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.yxzh.mapper.stockreviewsRepositoryDao] found for dependency [com.yxzh.mapper.stockreviewsRepositoryDao]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:

but when I run another .class

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

and implement 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);
    }
}

It worked well. It really confused me.

1
Is your stockreviewsRepositoryDao annotated with @Repository/@Component/@Service ?ByeBye
I tried,but it didn't work. So I deleted @Repository/@Component/@Service.Jason Ye

1 Answers

1
votes

Have you tried to annotate

stockreviewsRepositoryDao with @Repository

and

Application with @EnableJpaRepositories(basePackageClasses = {"stockreviewsRepositoryDao.class"})