首页 > 解决方案 > 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;
        }
}

标签: javaspring-mvcspring-bootinterfacespring-data-jpa

解决方案


UserServiceSpring在上下文中搜索实现并仅找到一个UserServiceImpl,如果您有 2 个,您将遇到可以使用配置文件或Qualifier


推荐阅读