Java 如何在Spring Boot中自动连接组件中的存储库接口
我正在使用Spring+Mysql,我可以在我的Java 如何在Spring Boot中自动连接组件中的存储库接口,java,spring,spring-boot,Java,Spring,Spring Boot,我正在使用Spring+Mysql,我可以在我的RepositoryRestController类中成功地自动连接从PagingAndSortingRepository扩展的类 我可以在下面的控制器中自动连接我的存储库 package com.fallavi.api.user.controller; import com.fallavi.api.MyConfig; import com.fallavi.api.purchase.model.Purchase; import com.fallav
RepositoryRestController
类中成功地自动连接从PagingAndSortingRepository
扩展的类
我可以在下面的控制器中自动连接我的存储库
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()));
}
}
为了在中调用UserCreditHelper
类,我在下面的控制器类示例中使用ApplicationContext
@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");
}
}
@RepositoryRestController
@请求映射(“/user”)
公共类UserCreditController{
@自动连线
私人阅览室阅览室阅览室;
@自动连线
私人用户Repository用户Repository;
@自动连线
私有采购存储库采购存储库;
@GetMapping(
value=“/UserHasCreditThough/{reader\u id}”,
headers=“Content Type=application/json”)
公众的反应足够可信(
@RequestHeader(value=“Authorization”)字符串令牌{
//com.fallavi.api.user.calculator是UserCreditHelper的包。
ApplicationContext ctx=新注释ConfigApplicationContext(“com.fallavi.api.user.calculator”);
返回响应正确(“测试”);
}
}
当我试图请求/userhascreditough/{reader\u id}
端点时,它总是给出错误
创建名为“userCreditHelper”的bean时出错:通过字段“usersRepository”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“com.fallavi.api.user.repository.UsersRepository”的合格bean可用:至少需要1个符合autowire候选条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}“,
您应该创建一个作为服务层的接口。然后将该接口注入控制器,并在控制器的desire端点调用desire方法。从这里,您可以从这个注入接口调用所需的植入。为什么只使用该附加包创建新的应用程序上下文?您应该将现有上下文配置为包含该包。不要…请不要。。。除非您想遇到奇怪的事务问题、内存问题、性能下降等。每次执行newannotationconfigapplicationcontext(“com.fallavi.api.user.calculator”)
时,您都在重新加载整个应用程序!。只需使用依赖注入,您正试图使事情变得比需要的更复杂。您不应该。。。只需自动连接所需的bean,并确保扫描所有其他内容。不要自己创建上下文。如使用自动连接所述。。。您也在自动连接存储库,为什么会有所不同。您不需要这样做,只需执行@Autowire UserCreditHelper
。UserCreditHelper已经是一个Springbean,因此您可以直接自动连接它,而不必通过appContext。欢迎光临。您可以检查有关spring引导服务的spring配置,以及如何为您喜欢的每个控制器实现所需的实现。这非常简单,他们也提供了很好的例子@穆斯塔法尔肯
@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");
}
}