Is possible to inject method with arguments in Spring?

user710818 picture user710818 · Apr 28, 2012 · Viewed 7.4k times · Source

From docs:

In the client class containing the method to be injected (the CommandManager in this case), the method that is to be 'injected' must have a signature of the following form:

<public|protected> [abstract] <return-type> theMethodName(no-arguments);

Does exists way to workaround this limitation?

Answer

Bruce Lowe picture Bruce Lowe · Apr 28, 2012

Yes you can. Here is an example from the spring docs, http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html

 @Autowired
 public void prepare(MovieCatalog movieCatalog,
                   CustomerPreferenceDao customerPreferenceDao) {
   this.movieCatalog = movieCatalog;
   this.customerPreferenceDao = customerPreferenceDao;
 }