0

I was trying to check some piece of code within spring framework where the autowired functionality is coded.

Let me give you an example.

@Component
public lass Service{
   @Autowired
   private DaoLayer daoLayer;
}

My question is: How spring can inject the DaoLayer or any instance when the respective field is marked using Autowired.

  1. Make this using reflection? , if reflection is used: Does reflection can access private fields?
  2. Make this using a new created proxy? and inject the references on it?, if this happens what happens when getters and setters are not present?
  3. Use other technique and which one?

Could anyone could point me where I can find this information or explain me more about this process.

I remember in sring2.5 getters and setter need to be present but no more in spring3.2, so now spring created them by its own? or are not used anymore?

Thanks.

Koitoer
  • 18,778
  • 7
  • 63
  • 86

1 Answers1

1

Make this using reflection? , if reflection is used: Does reflection can access private fields?

Yes, Spring uses reflection everywhere. With reflection you can do many things, like access private fields, methods, constructors, and classes.

Make this using a new created proxy? and inject the references on it?, if this happens what happens when getters and setters are not present?

Spring resolves @Autowired on fields directly. It only needs getters and setters (with Java bean conventional names) to resolve <property> elements of a <bean> declaration while creating the bean. Note that Spring again uses reflection to invoke these getters/setters.

Use other technique and which one?

Doesn't need anything else.

If you're interested in the actual class that does this, look into AutowiredAnnotationBeanPostProcessor. The Spring documentation explains some of this too (look into the IoC chapter).

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724