2020-02-14

Spring @Required

@Required 

The @Required annotation applies to bean property setter methods, as in the following example:

public class SimpleMovieLister { 
 private MovieFinder movieFinder; 
 @Required public void setMovieFinder(MovieFinder movieFinder) {
 this.movieFinder = movieFinder; 
 } // ...
}

This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on. It is still recommended that you put assertions into the bean class itself, for example, into an init method. Doing so enforces those required references and values even when you use the class outside of a container. 

No comments:

Post a Comment