views:

258

answers:

3

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.

A: 

Spring uses the CGLib API to provide autowired dependency injection.


References

Further Reading

krock
Unless I'm mistaken, @Autowired does not require any instrumentation, just reflection. Other annotations like @Transactional do require instrumentation.
Mike Q
+1  A: 

Java allows you to interact with private members of a class via reflection.

Check out ReflectionTestUtils, which is very handy for writing unit tests.

James Earl Douglas
+2  A: 

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). One the field can be discovered and written to, it's pretty trivial to do the rest of it; merely a Simple Matter Of Programming.

Donal Fellows
In case you hadn't guessed, I'm impressed with how slick the Spring guys have made their code. I know how they do it, but that doesn't change how good it is.
Donal Fellows