java - spring @autowired 如何用于接口和实现类
问题描述
我是 Java/Spring Boot 的新手,并且看到在 UserServiceImpl 类中被覆盖的方法的意外功能。即使没有导出此类,覆盖的方法也是正在使用的方法。
基本上,我有一个 UserService 接口类和这个接口的 UserServiceImpl 类。UserService 接口声明了一个 createUser 方法。UserServiceImpl 然后覆盖此方法并添加附加功能。
UserController 类然后导入 UserService 接口类并调用 createUser 方法。但是,即使没有将 UserServiceImpl 导入到 UserController 类中,也会使用该类中重写的 createUser 方法。如果重写的 impl 类没有导入到 UserController 中,那么 UserController 怎么可能知道接口中的 createUser 方法被重写了?
我在下面包括了这些类:
用户服务接口:
package sbootproject.service.intrf;
import sbootproject.shared.dto.UserDto;
public interface UserService {
UserDto createUser(UserDto user);
}
覆盖 createUser 方法的 UserService Impl:
package sbootproject.service.impl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sbootproject.UserRepository;
import sbootproject.entity.UserEntity;
import sbootproject.service.intrf.UserService;
import sbootproject.shared.dto.UserDto;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Override
public UserDto createUser(UserDto user) {
UserEntity userEntity = new UserEntity();
BeanUtils.copyProperties(user, userEntity);
userEntity.setEncryptedPassword("test");
userEntity.setUserId("testUserId");
UserEntity storedUserDetails = userRepository.save(userEntity);
UserDto returnValue = new UserDto();
BeanUtils.copyProperties(storedUserDetails, returnValue);
return returnValue;
}
}
最后,用户控制器:
package sbootproject.controller;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import sbootproject.model.request.UserDetailsRequestModel;
import sbootproject.model.response.UserRest;
import sbootproject.service.intrf.UserService;
import sbootproject.shared.dto.UserDto;
@RestController
public class UserController {
@Autowired
UserService userService;
@PostMapping(path="/postMethod")
public UserRest createUser(@RequestBody UserDetailsRequestModel userDetails) {
UserRest returnValue = new UserRest();
UserDto userDto = new UserDto();
BeanUtils.copyProperties(userDetails, userDto);
UserDto createdUser = userService.createUser(userDto);
BeanUtils.copyProperties(createdUser, returnValue);
return returnValue;
}
}
解决方案
UserService
Spring在上下文中搜索实现并仅找到一个UserServiceImpl
,如果您有 2 个,您将遇到可以使用配置文件或Qualifier
推荐阅读
- c - 这是在 C 中使用 memset() 的正确方法吗?
- amazon-web-services - 如何在内部 SharePoint 网站上发布 AWS Lex 机器人?
- gitlab - GitLab For Loop 为每个事物运行阶段
- arrays - 如何动态重新渲染分页控件?
- android - downloadmanager api 中的客户端证书
- python - 为什么 Visual Studio Code 认为“import pygame”是错误的?
- prolog - 使用列表的 Prolog 到 Datalog 代码转换
- php - PHP mariaDB更新数据库中的令牌
- lua - 无法匹配中间的零个或多个模式
- spacy - ModuleNotFoundError:即使 spacy 和 python 在同一路径中,也没有名为“spacy”的模块