• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Spring Interfaces Significance  RSS feed

 
Ranch Hand
Posts: 362
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Spring Core, there are couple of interfaces such as: BeanNameAware, BeanFactoryAware, ApplicationContextAware, PostProcessors etc. I am trying to understand the significance and use cases where we should use these interfaces. We already have init() method/constructor etc. where we can put any logic so what is the usecase for using these interfaces?

Also, why do we have init() method while we can already have a constructor for any bean?
 
Ranch Hand
Posts: 1703
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The init() method is to initialize some variables of a bean. I don't think developers won't need a constructor (or the new operator) to instantiate a bean.

The bean is created by injection using @Autowired to inject the bean. The container will create the bean, not the developer.
Also, a bean should have a non-argument constructor where no variables are initialized in this constructor.
init() method is called after the bean is constructed. Sometimes, we don't want some variables to be initialized during bean's instantiation.

For more information about the Aware interface, refer to this example https://www.baeldung.com/spring-bean-name-factory-aware
BeanPostProcessors are for call back method during a lifecycle of a bean, refer to these examples https://www.tutorialspoint.com/spring/spring_bean_post_processors.htm and https://stackoverflow.com/questions/29743320/how-exactly-does-the-spring-beanpostprocessor-work
 
Rancher
Posts: 977
9
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think these interfaces allow a developer to extend the functionality of the framework in particular to customise the bean lifecycle behaviour.
Here's a quick video; https://www.youtube.com/watch?v=6qGonNfth88
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:The init() method is to initialize some variables of a bean. I don't think developers won't need a constructor (or the new operator) to instantiate a bean.

The bean is created by injection using @Autowired to inject the bean. The container will create the bean, not the developer.
Also, a bean should have a non-argument constructor where no variables are initialized in this constructor.


That's not necessarily true. You can add a non-argument constructor, as long as all arguments can be injected by the container. That means the arguments must be other beans, or configuration properties, etc. The container will then call the constructor for you. You can use that same constructor to initialise all other necessary state.

You can even give a bean multiple constructors, but then you must annotate one of them with @Autowired to tell Spring which constructor to use when it must create the bean. The other constructors can be used in unit tests etc.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!