kk luo
kk luo

Reputation: 599

Error required a bean of type 'XXX' that could not be found

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.

Upvotes: 1

Views: 1021

Answers (3)

Faheem azaz Bhanej
Faheem azaz Bhanej

Reputation: 2396

We never put @Component (or @Service, ...) at an interface, because this make the interface useless. Let me explain why.

  • If you have an interface then you want to use that interface for the injection point type.

  • The purpose of an interface is that it define a contract that can been implemented by several implementations. On the other side you have your injection point (@Autowired). Having just one interface and only one class that implement it, is useless.

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.

Upvotes: 0

kamal
kamal

Reputation: 59

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)

Upvotes: 0

seyoatda
seyoatda

Reputation: 51

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

Upvotes: 1

Related Questions