Why the injection dependency of spring construction is immutable?

spring framework, why can dependency immutability be guaranteed by constructor injection?

Dec.08,2021

if I am not good at Chinese, I can't understand what dependence is immutable. It is your ability to make complicated things simple. It is a hoax to say that simple things are complex-this is common in the Chinese version of technical books.

back to your question, if it is constructor injection, you can only pass in the object when you create an instance, and there is no way to modify it after instance creation, while setter injection can call setter modification again. For example, after
constructor is injected into, new Zoo (monkey), monkey cannot be changed:
class Zoo {

public Zoo(Animal animal) {
}

}

After

setter is injected into, setAnimal (monkey), you can modify it by calling setAnimal (panda):
class Zoo {

public void setAnimal(Animal animal) {
}

}

Menu