最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決springboot無(wú)法注入JpaRepository的問(wèn)題

 更新時(shí)間:2021年01月29日 08:46:32   作者:shumoyin  
這篇文章主要介紹了解決springboot無(wú)法注入JpaRepository的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

使用內(nèi)置服務(wù)器啟動(dòng)springboot項(xiàng)目時(shí),會(huì)從@SpringBootApplication修飾類(lèi)所在的包開(kāi)始,加載當(dāng)前包和所有子包下的類(lèi),將由@Component @Repository @Service @Controller修飾的類(lèi)交由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;
@SpringBootApplication
public 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)類(lèi),并交由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
@Transactional
public 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指定掃描目錄也無(wú)法將HttpDocRepository成功注入,會(huì)產(chǎn)生如下錯(cuò)誤描述

Action:

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

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

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

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

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

測(cè)試通過(guò)

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

測(cè)試通過(guò)

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

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

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

相關(guān)文章

最新評(píng)論

桐城市| 嘉善县| 江西省| 余干县| 河东区| 巴东县| 万荣县| 格尔木市| 神农架林区| 翼城县| 陆丰市| 土默特右旗| 南和县| 永丰县| 乌拉特后旗| 介休市| 桐城市| 克拉玛依市| 禹城市| 尉氏县| 淄博市| 手机| 崇礼县| 罗甸县| 余江县| 广饶县| 华宁县| 龙门县| 永新县| 武定县| 天柱县| 石首市| 永修县| 凌海市| 和政县| 独山县| 保亭| 陕西省| 凭祥市| 拜泉县| 乐陵市|