亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

解決springboot無法注入JpaRepository的問題

瀏覽:3日期:2023-03-27 08:51:25

使用內置服務器啟動springboot項目時,會從@SpringBootApplication修飾類所在的包開始,加載當前包和所有子包下的類,將由@Component @Repository @Service @Controller修飾的類交由spring進行管理。

package com.facade;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.stereotype.Component;@SpringBootApplicationpublic class Application { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); String[] profiles = context.getEnvironment().getActiveProfiles(); if (profiles != null) { for (String profile : profiles) {System.out.println('------------start with profile : ' + profile); } } }}

在使用Spring data jpa時,通常都是繼承Repository接口相關的其他接口,然后Spring data jpa在項目啟動時,會為所有繼承了Repository的接口(@NoRepositoryBean修飾除外)創建實現類,并交由Spring管理。

例如,

package com.facade.repository;import org.springframework.data.repository.PagingAndSortingRepository;import com.facade.entity.HttpDoc;public interface HttpDocRepository extends PagingAndSortingRepository<HttpDoc, Long> {}

package com.facade.service;import com.facade.entity.HttpDoc;public interface HttpDocService { public HttpDoc save(HttpDoc entity); public HttpDoc getById(Long id); public Iterable<HttpDoc> findAll();}

package com.facade.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.facade.entity.HttpDoc;import com.facade.repository.HttpDocRepository;import com.facade.service.HttpDocService;@Service@Transactionalpublic class HttpDocServiceImpl implements HttpDocService { @Autowired private HttpDocRepository httpDocRepository; @Override public HttpDoc save(HttpDoc entity) { return httpDocRepository.save(entity); } @Override public HttpDoc getById(Long id) { return httpDocRepository.findOne(id); } @Override public Iterable<HttpDoc> findAll() { return httpDocRepository.findAll(); }}

以上代碼Application處于HttpDocRepository HttpDocServiceImpl的根目錄中,所以HttpDocRepository是可以被成功注入到HttpDocServiceImpl中的。

如果將Application移動到其他平行目錄或者子目錄,就算使用scanBasePackages指定掃描目錄也無法將HttpDocRepository成功注入,會產生如下錯誤描述

Action:

Consider defining a bean of type ’com.facade.repository.HttpDocRepository’ in your configuration.

補充:(親測好用的解決方法)springboot2.x整合jpaRepository中的坑

今日折騰的時候發現了一起在1.5的時候整合jpa可以使用的findOne方法突然找不到了,如下:

解決springboot無法注入JpaRepository的問題

可以看到這個方法里面不能傳入String/Integer類型的值,所以百度了一番。

有網友給了一個通過get()再取值的方法,測試了一番并無效果。通過瀏覽調用方法列表發現了一個getOne()的方法,返回值類型和傳遞的參數都符合就試了一下

解決springboot無法注入JpaRepository的問題

測試通過

解決springboot無法注入JpaRepository的問題

這是由于jpa懶加載的問題引起的,可以在測試關聯的實體類中添加@Proxy(lazy=false)解決

解決springboot無法注入JpaRepository的問題

測試通過

解決springboot無法注入JpaRepository的問題

順帶想著測試一下findById()的方法也發現了一個問題

解決springboot無法注入JpaRepository的問題

返回值變為了一個Optional<>,這個可以通過get()方法得到想要的類型值。

解決springboot無法注入JpaRepository的問題

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Spring
相關文章:
主站蜘蛛池模板: 黑人一级大毛片 | 欧美在线免费 | 亚洲欧美另类在线 | 特黄特色大片免费播放 | 美女一级毛片免费观看 | 狠狠综合欧美综合欧美色 | 国产一区二卡三区四区 | 国产精品精品国产一区二区 | 高h辣肉各种姿势爽文bl | 亚洲色啦啦狠狠网站 | 精品福利一区二区免费视频 | 香蕉乱码成人久久天堂爱免费 | 国产freexxxx性播放麻豆 | 在线免费观看精品 | 精品一久久香蕉国产二月 | 亚洲国产精品ⅴa在线观看 亚洲国产精品aaa一区 | 中文字幕一区视频一线 | 久久国产精品女 | 国产福利在线观看永久免费 | 久久久久久亚洲精品不卡 | 1024国产基地 | 欧美一区二区三区国产精品 | 欧美高清色视频在线播放 | 黄色一级片免费播放 | 日韩精品中文乱码在线观看 | 日本免费高清一级毛片 | 久久网免费视频 | 国产视频三区 | 自拍欧美日韩 | 青青草一区二区免费精品 | 日本免费乱人伦在线观看 | 在线观看免费黄视频 | 欧美黄色特级视频 | 国产精品久久久久久一区二区 | 欧美在线成人午夜网站 | 国产美女a做受大片在线观看 | 一级片在线视频 | 一级做a爱过程免费视频时看 | 热99re久久精品2久久久 | 国产亚洲欧美日韩在线观看一区二区 | 国产精品免费观看视频 |