Are the Spring @PostConstruct and @PreDestroy method's annotations annotations a form of AOP?

2.8k views Asked by At

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?

3

There are 3 answers

1
Maksym On BEST ANSWER

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.

1
Bond - Java Bond On

Both are part of EE specs and not of spring.

Please refer respective docs here and here for more details.

0
virgo47 On

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.