我经常看到在构造函数而不是变量本身上使用@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;
}
谢谢