I read (for example here) that if I use field dependency injection then I cannot create immutable objects, but I don't understand how field dependency injection breaks the immutability, I can do something like below can create immutable SpringTest
, no?
@Component
public final class SpringTest {
@Autowired
private Person person;
// here I will have no setter methods exposing "person" object but will only have getter methods for "person" object, ensuring defensive copying etc...
}
UPDATE: Please note that my question is specifically about field injection breaking the immutability, so I want to know how my above code snippet (changed since my original questions, thanks to @Andy and others for correcting it) is breaking the immutability, I think it is not, so I think this answer is wrong about immutability with field injection.