I have a question with respect to Spring , please excuse if my Question is a dumb one .
Please see this code
public class HomePageController extends AbstractController {
private GeekNewsService service;
protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception {
List<NewsArticle> articles = service.getArticleOverviews();
return new ModelAndView( "home", "articles", articles );
}
public void setGeekNewsService( GeekNewsService service ) {
this.service = service;
}
}
Here my question is that , inside the handleRequestInternal Method , why there wasn’t any NullPointerException at this line service.getArticleOverviews() ?? ( Because the actual Object Creation will happen with the help of SetterInjection at setMethod )??
Is there any rule in Spring that , when a class has been called the setXXX Methods must be invoked at first ??
Yes, this is the basic contract Spring is giving to you: before it returns any bean you are guaranteed that this bean is initialized based on your configuration. In other words you will never be capable of calling any business method of any bean prior to full initialization of that bean (setter injection, field injection, post construction callbacks).