Java spring@autowired如何为接口和实现类工作
我不熟悉Java/Spring引导,并且看到了在UserServiceImpl类中重写的方法的意外功能。即使未导出此类,被重写的方法也是正在使用的方法 基本上,这个接口有一个UserService接口类和一个UserServiceImpl类。UserService接口已声明createUser方法。然后,UserServiceImpl覆盖此方法并添加其他功能 然后UserController类导入UserService接口类并调用createUser方法。但是,即使UserServiceImpl未导入UserController类,也会使用该类中被重写的createUser方法。如果在其中重写的impl类未导入到UserController中,UserController如何可能知道接口中的createUser方法已被重写 我将这些课程包括在下面: 用户服务接口:Java spring@autowired如何为接口和实现类工作,java,spring-mvc,spring-boot,interface,spring-data-jpa,Java,Spring Mvc,Spring Boot,Interface,Spring Data Jpa,我不熟悉Java/Spring引导,并且看到了在UserServiceImpl类中重写的方法的意外功能。即使未导出此类,被重写的方法也是正在使用的方法 基本上,这个接口有一个UserService接口类和一个UserServiceImpl类。UserService接口已声明createUser方法。然后,UserServiceImpl覆盖此方法并添加其他功能 然后UserController类导入UserService接口类并调用createUser方法。但是,即使UserServiceImpl
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;
}
}
最后,UserController:
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;
}
}
Spring在上下文中搜索实现
UserService
,只找到一个UserServiceImpl
,如果您有两个,您将有一个问题可以通过使用配置文件或限定符来解决在上下文中搜索实现UserService
,只找到一个UserServiceImpl
,如果您有2个,您将有一个问题,可以使用配置文件或限定符来解决。如果接口只有一个实现,并且在启用Spring组件扫描的情况下,使用@Component或@service进行注释,Spring framework可以找到(接口,实现)对
@限定词
如果有多个实现,则需要@Qualifier annotation和@Autowired annotation一起注入正确的实现。如果接口只有一个实现,并且该实现使用@Component或@service进行了注释,并启用了Spring的组件扫描,则Spring framework可以找到(接口、实现)对
@限定词
如果有多个实现,则需要@Qualifier annotation和@Autowired annotation一起注入正确的实现。Spring知道,因为它是实现接口的唯一类?Spring知道,因为它是实现接口的唯一类?