Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @autowired annotation works for a private field? [duplicate]

Tags:

java

spring

How does @Autowired annotation work for a private field without a getter and a setter? How can spring access a private field?

like image 687
Vishnudev K Avatar asked Feb 24 '15 11:02

Vishnudev K


1 Answers

It works with reflection. Here you can find an example of how to set public fields. But setting private fields does not make much of a difference

The only difference with a private field is that you will need to set the setAccessible before you are able to read/write to the field. Further, this method can throw a SecurityException. Java Docs

like image 182
wastl Avatar answered Nov 10 '22 16:11

wastl