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?
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.
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.
@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.
You can use
@PostConstruct
public void init() {
// ...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With