簡體   English   中英

spring @autowired如何為接口和實現類工作

[英]How spring @autowired works for interface and implementation classes

我是Java / Spring Boot的新手,正在看到UserServiceImpl類中重寫的方法的意外功能。 即使未導出此類,但覆蓋的方法仍在使用。

基本上,我有一個UserService接口類和該接口的UserServiceImpl類。 UserService接口具有聲明的createUser方法。 然后,UserServiceImpl覆蓋此方法並添加其他功能。

然后,UserController類導入UserService Interface類,並調用createUser方法。 但是,即使沒有將UserServiceImpl導入到UserController類中,也會使用從此類重寫的createUser方法。 如果未將覆蓋其中的impl類導入到UserController中,則UserController如何知道接口中的createUser方法已被覆蓋?

我在下面包括了這些類:

UserService接口:

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 ,如果您有2個,則可能會遇到一個問題,可以使用配置文件或Qualifier

如果只有一個接口實現,並且在啟用了Spring的組件掃描的情況下使用@Component或@service進行了注釋,則Spring框架可以找出(接口,實現)對。

@Qualifier

如果您有多個實現,則需要@Qualifier批注以及@Autowired批注注入正確的實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM