How does Spring annotation @Autowired work?

Anthony picture Anthony · Aug 21, 2010 · Viewed 32.1k times · Source

I came across an example of @Autowired:

public class EmpManager {
   @Autowired
   private EmpDao empDao;
}

I was curious about how the empDao get sets since there are no setter methods and it is private.

Answer

Donal Fellows picture Donal Fellows · Aug 21, 2010

Java allows access controls on a field or method to be turned off (yes, there's a security check to pass first) via the AccessibleObject.setAccessible() method which is part of the reflection framework (both Field and Method inherit from AccessibleObject). Once the field can be discovered and written to, it's pretty trivial to do the rest of it; merely a Simple Matter Of Programming.