簡體   English   中英

為什么我的springboot應用程序看不到@Service批注?

[英]why my springboot app cant see @Service annotation?

我的服務等級

package poklakni.library.service;
import java.util.List;
import java.util.function.Predicate;
import org.springframework.stereotype.Service;
import poklakni.library.entity.Book;

@Service
public interface BookService {

    //some crud methods
}

主班

package poklakni.library;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import poklakni.library.repository.BookRepository;
import poklakni.library.repository.PersonRepository;
import poklakni.library.service.BookService;
import poklakni.library.service.PersonService;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private BookService bookService;

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

    @Override
    public void run(String... args) throws Exception {

        //more code
    }
}

它說

***************************
APPLICATION FAILED TO START
***************************
Description:

Field bookService in poklakni.library.Application required a bean of type 
'poklakni.library.service.BookService' that could not be found.

Action:

Consider defining a bean of type 'poklakni.library.service.BookService' in 
your configuration.

即使我添加@ComponentScan(“ poklakni.library”),它也不起作用

我也使用@Repository批注進行回購,它可以完美地自動工作,但是服務不起作用我在做什么錯呢? 謝謝你的任何建議

編輯:還有一個服務包poklakni.library.service的實現;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import poklakni.library.entity.Book;
import poklakni.library.repository.BookRepository;
import poklakni.library.repository.PersonRepository;

public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepo;

    //more code
}

BookService是一個接口,無法實例化。 @Service應該放在實現BookService的具體類上。

請用@Service注釋BookServiceImpl

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepo;

    //more code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM