java - Spring MVC multiple controllers of the same class -
i want have 1 controller class, 4 instances of it, each of instance have own datasource , controller path, else (methods, validations rules, views names) same;
so need :
class mycontroller{ private myservice service; @requestmapping("somework") public string handlerequest(){ ........ } .................... }
configuration class :
@configuration @enablewebmvc public class appconfiguration { @controller // assuming exists @requestmapping('con1') // desired result mycontroller controller1(){ mycontroller con = new mycontroller(); con.setservice(service1bean); return con; } @controller // assuming exists @requestmapping('con2') // desired result mycontroller controller2(){ mycontroller con = new mycontroller(); con.setservice(service2bean); return con; } ............................... }
no, can't this.
first, annotations set in stone @ compile time. constant meta data cannot modify. though, accessible @ run time through reflection, cannot modify them.
second, @controller
annotation call used annotate types. cannot use on method. there no corresponding annotation in spring mvc want in example. (you write own.)
finally, spring mvc stack registers @controller
beans' methods handlers mapping them various url patterns provide. if tries register pattern has been registered, fails because duplicate mappings not allowed.
consider refactoring. create @controller
class each path want move logic @service
bean can customize use whatever data source need.
Comments
Post a Comment