猿问

如何在 Spring Boot 中的组件中自动装配存储库接口

我使用Spring + Mysql的,我可以自动装配成功我的课,从扩展PagingAndSortingRepository<T,E>在我的RepositoryRestController课。


我可以在下面的控制器中自动连接我的存储库。


package com.fallavi.api.user.controller;


import com.fallavi.api.MyConfig;

import com.fallavi.api.purchase.model.Purchase;

import com.fallavi.api.purchase.repository.PurchaseRepository;

import com.fallavi.api.reader.model.Reader;

import com.fallavi.api.reader.repository.ReaderRepository;

import com.fallavi.api.user.calculator.UserCreditHelper;

import com.fallavi.api.user.exceptions.UserCanNotFindException;

import com.fallavi.api.user.model.UserCreditEnoughModel;

import com.fallavi.api.user.model.UserCreditModel;

import com.fallavi.api.user.repository.UsersRepository;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;

import org.springframework.data.rest.webmvc.RepositoryRestController;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestHeader;

import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;


@RepositoryRestController

@RequestMapping("/user")

public class UserCreditController {


    @Autowired

    private ReaderRepository readerRepository;


    @Autowired

    private UsersRepository usersRepository;


    @Autowired

    private PurchaseRepository purchaseRepository;


    @GetMapping(

            value = "/userHasCreditEnough/{reader_id}",

            headers = "Content-Type=application/json")

    public ResponseEntity<UserCreditEnoughModel> userHasCreditEnough(

            @RequestHeader(value = "Authorization") String token,

    }

}


慕桂英4014372
浏览 100回答 1
1回答

千巷猫影

您应该创建一个接口作为您的服务层。然后将该接口注入控制器并在控制器的期望端点中调用期望方法。从那里从这个注入的接口调用你想要的植入。
随时随地看视频慕课网APP

相关分类

Java
我要回答