久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

解決springboot無法注入JpaRepository的問題

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

使用內(nèi)置服務(wù)器啟動(dòng)springboot項(xiàng)目時(shí),會(huì)從@SpringBootApplication修飾類所在的包開始,加載當(dāng)前包和所有子包下的類,將由@Component @Repository @Service @Controller修飾的類交由spring進(jìn)行管理。

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時(shí),通常都是繼承Repository接口相關(guān)的其他接口,然后Spring data jpa在項(xiàng)目啟動(dòng)時(shí),會(huì)為所有繼承了Repository的接口(@NoRepositoryBean修飾除外)創(chuàng)建實(shí)現(xiàn)類,并交由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移動(dòng)到其他平行目錄或者子目錄,就算使用scanBasePackages指定掃描目錄也無法將HttpDocRepository成功注入,會(huì)產(chǎn)生如下錯(cuò)誤描述

Action:

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

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

今日折騰的時(shí)候發(fā)現(xiàn)了一起在1.5的時(shí)候整合jpa可以使用的findOne方法突然找不到了,如下:

解決springboot無法注入JpaRepository的問題

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

有網(wǎng)友給了一個(gè)通過get()再取值的方法,測試了一番并無效果。通過瀏覽調(diào)用方法列表發(fā)現(xiàn)了一個(gè)getOne()的方法,返回值類型和傳遞的參數(shù)都符合就試了一下

解決springboot無法注入JpaRepository的問題

測試通過

解決springboot無法注入JpaRepository的問題

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

解決springboot無法注入JpaRepository的問題

測試通過

解決springboot無法注入JpaRepository的問題

順帶想著測試一下findById()的方法也發(fā)現(xiàn)了一個(gè)問題

解決springboot無法注入JpaRepository的問題

返回值變?yōu)榱艘粋€(gè)Optional<>,這個(gè)可以通過get()方法得到想要的類型值。

解決springboot無法注入JpaRepository的問題

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产成人在线播放 | 91av国产在线视频 | 精品久久久久久久久久久 | 天堂中文网| 国产免费一区二区三区四区五区 | 欧美二区三区视频 | 精品自拍视频 | 午夜视频网 | 国产精品九九九 | caoporn免费在线视频 | 成人h视频在线观看 | 国产福利电影一区 | 国产丝袜一区 | jav成人av免费播放 | 久久精品久久久 | 久久网一区二区三区 | av观看免费 | 天天操网址 | 可以免费观看的av | 欧美黄色大片网站 | 久久精品福利 | 黄p在线看| 精品久久久久久亚洲精品 | 嫩草视频在线观看免费 | 日韩成人av在线 | 国产成人精品久久 | 免费网站国产 | 亚洲一区二区国产 | 风间由美一区二区三区在线观看 | 中文字幕 国产精品 | 日韩在线资源 | 天天澡天天狠天天天做 | 国产区在线 | 色接久久 | 久久一区| 一级片在线免费看 | 久久一区| 国产成人一区 | 久久久91精品国产一区二区 | 欧美日韩黄色一区二区 | 日韩欧美在线观看一区二区三区 |