52

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.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Anthony
  • 1,685
  • 4
  • 22
  • 29
  • 2
    Look at this post http://stackoverflow.com/questions/3153546/how-does-autowiring-work-in-spring – Pramod Kumar Aug 06 '12 at 08:42
  • Possible duplicate of [Understanding Spring @Autowired usage](https://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage) – tkruse Dec 06 '17 at 05:11

4 Answers4

46

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.

Leonardo Pina
  • 458
  • 1
  • 7
  • 17
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 3
    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 Aug 21 '10 at 16:16
  • I've come across code that doesn't get autowired properties injected until I add a public setter method. What would cause that? – Snekse Dec 29 '11 at 16:38
  • @Snekse: No idea offhand. Ask it as a full question, _including the exact version of Spring used_ and _the code in question_ and maybe — probably — someone will be able to tell you. – Donal Fellows Dec 31 '11 at 17:36
  • It's probably due to Java security policy which is set so that it disables access to private fields using reflection. – Ondrej Bozek May 10 '13 at 08:02
  • if i am getting your answer , just cross checking it.. you mean that - access controls are auto turned off then what is the need of private access specifier in a class if it is @Autowired – Aman Nov 05 '17 at 15:22
  • If it's so simple then give an example. – Philip Rego Mar 14 '18 at 14:12
7

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

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

earldouglas
  • 13,265
  • 5
  • 41
  • 50
4

No need for any setter, you just have to declare the EmpDao class with the annotation @component in order that Spring identifies it as part of the components which are contained in the ApplicationContext ...

You have 2 solutions:

  • To manually declare your beans in the XML file applicationContext :
<bean class="package.EmpDao" />
  • To use automatic detection by seeting these lines in your context file:
<context:component-scan base-package="package" />
<context:annotation-config />

AND to use the spring annotation to declare the classes that your spring container will manage as components:

@Component
class EmpDao {...}

AND to annotate its reference by @Autowired:

@Component (or @Controller, or @Service...)
class myClass {

// tells the application context to inject an instance of EmpDao here
@Autowired
EmpDao empDao;


public void useMyDao()
{
    empDao.method();
}
...
}

Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. Both classes should be beans, i.e. they should be defined to live in the application context.

Spring knows the existence of the beans EmpDao and MyClass and will instantiate automatically an instance of EmpDao in MyClass.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
MrJavaJEE
  • 41
  • 2
1

Spring uses the CGLib API to provide autowired dependency injection.


References

Further Reading

krock
  • 28,904
  • 13
  • 79
  • 85
  • 2
    Unless I'm mistaken, @Autowired does not require any instrumentation, just reflection. Other annotations like @Transactional do require instrumentation. – Mike Q Aug 25 '10 at 22:29
  • I agree, @Autowired should not use CGLib. Only factory injection or proxy classes etc. – Nakedible Mar 25 '11 at 13:59