我將我的 DAO 重新配置為一種更方便的方式(通過使用 JpaRepository),而不是手動執行所有樣板代碼。但是現在每次我啟動 Spring Application 它都會給我以下錯誤:APPLICATION FAILED TO START Description: Field userRepository in DAO.UserDAOService required a bean of type 'DAO.UserRepository' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action:Consider defining a bean of type 'DAO.UserRepository' in your configuration.Process finished with exit code 1
5 回答

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
解決方案:只需在 Spring 應用程序所在的同一包中創建子包。
可以在此處找到解決方案示例:“字段需要找不到類型的 bean?!?nbsp;使用mongodb的錯誤spring restful API

侃侃無極
TA貢獻2051條經驗 獲得超10個贊
添加 @Repository 注釋,然后 bean 將在服務中創建并自動裝配。
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User , Integer>
{
}
并且不需要在服務中創建bean
@Bean
public void setUserRepository(UserRepository userRepository)
{
this.userRepository = userRepository;
}

偶然的你
TA貢獻1841條經驗 獲得超3個贊
除了前面的答案之外,IDE 經常會建議您錯誤導入 Bean 類的注解,例如對于 @Service 注釋的 bean,請確保您導入:
import org.springframework.stereotype.Service;
而不是這樣的:
import org.jvnet.hk2.annotations.Service
添加回答
舉報
0/150
提交
取消