Home > Software design >  Error required a bean of type 'XXX' that could not be found
Error required a bean of type 'XXX' that could not be found

Time:04-07

I'm updating a spring boot project by adding a page called news and it has error below.

Field newsService in com.lrs.admin.controller.NewsController required a bean of type 'com.lrs.admin.service.INewsService' that could not be found.

The code is like below: Application.java

@MapperScan("com.lrs.admin.dao")
@ServletComponentScan
@SpringBootApplication
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{

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

NewController.java

@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {
    private final static String qxurl="news/list";
    
    @Autowired
    private INewsService newsService;

INewsService.java

package com.lrs.admin.service;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Componnt;
import com.lrs.admin.util.ParameterMap;

    @Component
    public interface INewsService {
        public List<ParameterMap> list();
        public Map<String,Object> getNews(ParameterMap pm);
        public Map<String,Object> edit(ParameterMap pm); 
        public Map<String,Object> add(ParameterMap pm,HttpSession session); 
        public Map<String,Object> del(String newsId); 
    }

NewsService.java

public class NewsService implements INewsService {

    @Autowired
    private NewsDao newsDao;
    

NewsDao.java

public interface NewsDao {
    public List<ParameterMap> list();
    public List<ParameterMap> getAllNewsById(ParameterMap pm);
    public ParameterMap getNewsById(ParameterMap pm);
    public void updateNews(ParameterMap pm);
    public void addNews(ParameterMap pm);
    public void delNews(String roleId);
//  public void delUserRole(String roleId);
}

NewsMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lrs.admin.dao.NewsDao">

</mapper>

I just create those files and not implement any method yet.Is this the reason? Any help?Thx.

CodePudding user response:

you should annotate @Service on the class which implements the interface instead of the interface itself. That is, annotate the NewsService with @Service, and you can remove the @Component tag from INewsService

CodePudding user response:

You have to remove @Component annotation from INewsService and add @Service annotaion in NewsService class because Spring tries to instantiate it to create a Bean @Service, but it's an Interface, so thats not possible.

CodePudding user response:

Try to delete this @Component from your INewsService.java and add @Service to NewsService.java it will resolve the error and be more comprehensive, cause @Component is a generalization of @Repository, @Service, and @Controller.

@Component : generic stereotype for any Spring-managed component

@Repository : stereotype for persistence layer

@Service : stereotype for service layer

@Controller : stereotype for presentation layer (spring-mvc)

  • Related