2

I have the following doubt related to the Spring @PostConstruct and @PreDestroy method's annotations.

So the @PostConstruct annotation means that the annoted method is automatically performed after the object creation and after the dependency injection done using the setters metthod

The @PreDestroy is automatically performed before when the ApplicationContext is closed.

My doubt is: are the @PostConstruct and @PreDestroy annotations a form of AOP or not?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

3 Answers3

3

Both are part of EE specs and not of spring.

Please refer respective docs here and here for more details.

Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
  • Both annotations are defined as part of the J2EE specification. However, Spring allows you to use those same standard annotations for its own (non EE) purposes. – Adam Batkin May 07 '15 at 15:40
2

AOP is a programming paradigm, see here. If I understand your question correctly you are asking are the "@PostConstruct and @PreDestroy" in scope of AOP. My answer would be yes , at least because they are developed with using Reflection that isn't OOP.

Note:

AOP includes programming methods and tools that support the modularization of concerns at the level of the source code.

Maksym
  • 4,434
  • 4
  • 27
  • 46
  • Tnx, I mean that it seems to me that these 2 annotations are in AOP scope because when it is performed a specific event (after the object construction and the setter inijection for the @PostConstruct and before the ApplicationContext is closed for @PreDestroy) the annotated method is automatucally performed. Is my reasoning correct? – AndreaNobili Nov 17 '14 at 13:26
0

As mentioned by Bond, these are Java EE annotations, not Spring ones - however as with many annotations, Spring supports them (just like Springs @Autowired vs EE @Inject, etc.).

I would NOT say, however, that reflection is not OOP - while it possibly goes against pure encapsulation, it is integral part of major OOP languages for good reasons. Reflection usage does not imply AOP either.

AOP is typically used to call something before/after/around the method - the code that is not visible there - and is often represented by an annotation (like Springs @Transactional), but can be also declared elsewhere (configuration). @PostConstruct and @PreDestroy on the other hand are primarily life-cycle methods, kind of hooks that will happen at specific time - and anything they do is explicitly in the code.

So there is no aspect hidden in there, definitely no aspect in the typical AOP meaning. The only thing hidden is the magic that calls it in the right moment. But while Java does not support AOP directly, no AOP library is needed, just simple reflection. I doubt anybody would call JVM's shutdown hooks AOP either.

virgo47
  • 2,283
  • 24
  • 30