[英]why my springboot app cant see @Service annotation?
My service class 我的服务等级
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
}
main class 主班
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
}
}
it says this 它说
***************************
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.
even if i add @ComponentScan ("poklakni.library") it doesnt work 即使我添加@ComponentScan(“ poklakni.library”),它也不起作用
i have also repo with @Repository annotation and it autowries it perfectly but service doesnt work what am I doing wrong? 我也使用@Repository批注进行回购,它可以完美地自动工作,但是服务不起作用我在做什么错呢? thank u for any advice
谢谢你的任何建议
EDIT: there is also a implementation of service package poklakni.library.service; 编辑:还有一个服务包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
is an interface and cannot be instantiated. BookService
是一个接口,无法实例化。 @Service
should be put on the concrete class that implements BookService
. @Service
应该放在实现BookService
的具体类上。
Please annotate you BookServiceImpl as @Service 请用@Service注释BookServiceImpl
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepo;
//more code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.