Java 春天找不到豆子

Java 春天找不到豆子,java,spring,spring-mvc,gradle,Java,Spring,Spring Mvc,Gradle,我正在学习Spring,因为它似乎是一个非常强大的框架。我已经做了很多入门指南,现在我正在尝试。在它里面,所有的类都放在同一个包中,但是为了让它更有趣,我尝试根据类(实体、控制器等)使用不同的包。在测试REST服务之前,我正要测试它,但在构建应用程序时出错。这就是我的项目的结构: 与本教程中的类唯一不同的是带有标记的servletializer,它与随附的(实际上我使用的是随STS附带的,但它是相同的)。据我所知,这与问题无关,所以这门课的内容与问题无关 本教程的另一个细微差别是这里的应用程序

我正在学习Spring,因为它似乎是一个非常强大的框架。我已经做了很多入门指南,现在我正在尝试。在它里面,所有的类都放在同一个包中,但是为了让它更有趣,我尝试根据类(实体、控制器等)使用不同的包。在测试REST服务之前,我正要测试它,但在构建应用程序时出错。这就是我的项目的结构:

与本教程中的类唯一不同的是带有标记的
servletializer
,它与随附的(实际上我使用的是随STS附带的,但它是相同的)。据我所知,这与问题无关,所以这门课的内容与问题无关

本教程的另一个细微差别是这里的
应用程序
类称为
RestServicesApplication
,但内容相同

当我尝试构建应用程序时(使用Gradle的
bootRun
而不是Maven),我收到了以下错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method init in com.example.restservices.RestServicesApplication required a bean of type 'com.example.repository.AccountRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.repository.AccountRepository' in your configuration.

:bootRun FAILED
因此,我尝试使用
@Bean
AccountRepository
进行注释,但它给出了一个编译错误,表示该位置不允许使用注释。接下来,我尝试使用
@Component
注释(也在
bookmarksrepository
上)并在
RestServicesApplication
中添加
@ComponentScan(“com.example”)
。之后,错误仍然存在,但消息更改为

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

Parameter 0 of constructor in com.example.controller.BookmarkRestController required a bean of type 'com.example.repository.BookmarkRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.repository.BookmarkRepository' in your configuration.

:bootRun FAILED
我在
bookmarksrestcontroller
中添加了
@Component
注释,但仍然存在相同的错误消息。我错过了什么

提前感谢您的回答


编辑#1 问题涉及的类如下(从我的项目中复制,而不是教程中的类,尽管差异很小):

RestServicesApplication
bookmarksrestcontroller
AccountRepository
注意:我添加了导入,以便您可以查看类和注释的来源


编辑#2
我尝试了另一件事:我重构了我的项目以适应教程,我将所有内容都放在同一个包中(
com.example.bookmarks
),并删除了额外的注释。该项目是编译的,但当我运行该项目时,在尝试访问REST服务时,我会获得
404
HTTP状态。我仍然对使用我的原始结构运行它感兴趣,但我想让您知道,这种重构使项目能够工作。

要让Spring创建一个实现JpaRepository接口的bean,您需要使用Spring JPA名称空间,并使用适当的元素激活存储库支持。在xml中:

<jpa:repositories base-package="com.example.repository" />


这将扫描
com.example.repository
下面的所有包,查找扩展
JpaRepository
的接口,并为其创建一个Springbean,该Springbean由的实现支持

我认为您必须再次创建包。你的包装看起来不对劲。重新创建您的包

您可以发布BookmarkRepository和AccountRepository类吗?您应该用@Controller注释标记*控制器类吗?服务、组件、控制器和存储库注释之间存在一些差异。阅读spring文档以获得详细解释。@harshavmb这两个存储库类与您在教程中看到的相同,只是添加了
@org.springframework.stereotype.Component
注释
bookmarksrestcontroller
确实是用
@org.springframework.web.bind.annotation.RestController
注释的,其中包括
@Controller
,我不会查看引用的链接/教程,你不应该期望其他人这样做。请参阅改进SO上的提问。好的。从两个*存储库接口,将组件注释替换为存储库注释。事实上,您不需要指定任何注释,因为它扩展了JpaRepository,spring应该创建bean。你能试试吗???看一下本教程,我遵循了本教程,唯一有意义的区别是存储库界面中的
@Transactional
注释(
应用程序.properties
不重要,因为我面临的是DI问题,而不是连接问题)。我尝试了
@javax.transaction.Transactional
@org.springframework.transaction.annotation.Transactional
,但这些都不适用于meCheck Edit#2,我更改了包,但服务不起作用,我的包出了什么问题?
package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.repository.AccountRepository;
import com.example.repository.BookmarkRepository;

@RestController
@RequestMapping("/{userId}/bookmarks")
public class BookmarkRestController {

    private final BookmarkRepository bookmarkRepository;
    private final AccountRepository accountRepository;

    @Autowired
    public BookmarkRestController(final BookmarkRepository bookmarkRepository,
            final AccountRepository accountRepository) {
        this.bookmarkRepository = bookmarkRepository;
        this.accountRepository = accountRepository;
    }

    // @RequestMapping methods...
}
package com.example.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

import com.example.entity.Account;

@Component
public interface AccountRepository extends JpaRepository<Account, Long> {
    Optional<Account> findByUsername(String username);
}
package com.example.repository;

import java.util.Collection;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

import com.example.entity.Bookmark;

@Component
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
    Collection<Bookmark> findByAccountUsername(String username);
}
<jpa:repositories base-package="com.example.repository" />
@EnableJpaRepositories