java - 为什么我的 springboot 应用看不到 @Service 注解?

标签 java hibernate spring-boot javabeans autowired

我的服务等级

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
}

最佳答案

请将BookServiceImpl注释为@Service

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepo;

    //more code
}

关于java - 为什么我的 springboot 应用看不到 @Service 注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581876/

相关文章:

Hibernate内存数据+数据库

java - 在另一个元素中使用显式等待查找元素

java - 哪种访问 berkeley db 的方法更好

java - 发布时不编译代码块?

java - 如何修复我的第一个 Hibernate 项目的错误?

Spring 启动 : NoUniqueBeanDefinitionException between test and main

Java 游戏开发 : Checking if player is in-between two y coords

java - 如何解决异常 “com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection”?

java - 基础 - 排除 Hibernate/JDBC 连接池问题

java - 仅在 JWT token 中添加附加信息,而不是在 OAuth2 token 中