Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Init method in Spring Controller (annotation version)

I'm converting a controller to the newer annotation version. In the old version I used to specify the init method in springmvc-servlet.xml using:

<beans>
    <bean id="myBean" class="..." init-method="init"/>
</beans>

How can I specify the init method using the annotation version?

like image 827
Krt_Malta Avatar asked Oct 16 '22 14:10

Krt_Malta


People also ask

What is @model annotation in Spring?

In Spring MVC, the @ModelAttribute annotation binds a method parameter or method return value to a named model attribute and then exposes it to a web view. It refers to the property of the Model object.

What type of Spring annotation is controller?

The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.

In what order do the @PostConstruct annotated method the Init method?

@PostConstruct , init-method are BeanPostProcessors. @PostConstruct is a JSR-250 annotation while init-method is Spring's way of having an initializing method. If you have a @PostConstruct method, this will be called first before the initializing methods are called.


2 Answers

You can use

@PostConstruct
public void init() {
   // ...
}
like image 270
Johan Sjöberg Avatar answered Oct 19 '22 03:10

Johan Sjöberg


Alternatively you can have your class implement the InitializingBean interface to provide a callback function (afterPropertiesSet()) which the ApplicationContext will invoke when the bean is constructed.

like image 21
matt b Avatar answered Oct 19 '22 04:10

matt b