gpt4 book ai didi

java - 如何在 Spring Boot 中的组件中 Autowiring 存储库接口(interface)

转载 作者:行者123 更新时间:2023-11-30 05:56:22 24 4
gpt4 key购买 nike

我正在使用 Spring + Mysql,我可以成功 Autowiring 从 PagingAndSortingRepository<T,E> 扩展的类。在我的RepositoryRestController类。

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

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,
@PathVariable Long reader_id) {

UserCreditHelper userCreditHelper = new UserCreditHelper();

// Find user ID from authorization code START //
Long userID = usersRepository.getUserIDByAuthToken(token);
if (userID == null) {
throw new UserCanNotFindException();
}
// Find user ID from authorization code END //

// Find user credit START //
List<Purchase> purchaseList = this.purchaseRepository.findByUserID(userID);
Integer userCreditLeft = userCreditHelper.userCreditLeft(purchaseList);
// Find user credit END //

// Find user's credit left for Reader START //
Reader reader = this.readerRepository.findByReaderID(reader_id);
boolean isUserCreditEnough = userCreditHelper.userHasCreditEnough(userCreditLeft, reader);
// Find user's credit left for Reader END //

return ResponseEntity.ok(new UserCreditEnoughModel(isUserCreditEnough, reader.getOnline()));
}
}

当然,我想将所有层分离为服务、存储库和 Controller ,这就是我创建一个新的帮助程序类(即服务层)的原因。

@Service
public class UserCreditHelper {

@Autowired
UsersRepository usersRepository;

....some methods...
}

为了调用UserCreditHelper我正在使用 ApplicationContext 中的类(class)在下面我的 Controller 类示例中。

@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<String> userHasCreditEnough(
@RequestHeader(value = "Authorization") String token) {

// com.fallavi.api.user.calculator is the package of UserCreditHelper.
ApplicationContext ctx = new AnnotationConfigApplicationContext( "com.fallavi.api.user.calculator" );

return ResponseEntity.ok("test");
}
}

当我尝试请求/userHasCreditEnough/{reader_id}时端点总是给出错误。

"Error creating bean with name 'userCreditHelper': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fallavi.api.user.repository.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}",

最佳答案

您应该创建一个接口(interface)作为您的服务层。然后将该接口(interface)注入(inject)到 Controller 中,并在 Controller 的所需端点中调用所需的方法。从那里从这个注入(inject)的接口(interface)调用您想要的植入。

关于java - 如何在 Spring Boot 中的组件中 Autowiring 存储库接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53097103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com