在构造函数上自动连线

时间:2019-01-10 18:03:06

标签: spring autowired

我经常看到在构造函数而不是变量本身上使用@Autowired注释:

@Service
public class AddressServiceImpl implements AddressService {

    private AddressRepository addressRepository;
    private UserRepository userRepository;

    @Autowired
    public AddressServiceImpl(AddressRepository addressRepository,
            UserRepository userRepository) {
        this.addressRepository = addressRepository;
        this.userRepository = userRepository;
    }
}

与下面的代码不同吗?

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressRepository addressRepository;
    @Autowired
    private UserRepository userRepository;
}

谢谢

0 个答案:

没有答案