java - Working with multiple dispatcher servlets in a spring application -
in spring application, have following configuration classes spring environment:
webappinitializer.java
@order(value=1) public class webappinitializer implements webapplicationinitializer { @suppresswarnings("resource") @override public void onstartup(servletcontext container) { // create 'root' spring application context annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); rootcontext.register(webappconfig.class); // manage lifecycle of root application context //container.addlistener(new contextloaderlistener(rootcontext)); // create dispatcher servlet's spring application context annotationconfigwebapplicationcontext dispatchercontext = new annotationconfigwebapplicationcontext(); dispatchercontext.register(dispatcherconfig.class); // register , map dispatcher servlet servletregistration.dynamic dispatcher = container.addservlet("dispatcher", new dispatcherservlet(dispatchercontext)); dispatcher.setloadonstartup(1); dispatcher.addmapping("/"); } }
webappconfig.java
@enablewebmvc @enabletransactionmanagement(mode=advicemode.proxy, proxytargetclass=true) @componentscan(value="spring.webapp.lojavirtual") @configuration public class webappconfig extends webmvcconfigureradapter { @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/bootstrap/**").addresourcelocations("/bootstrap/").setcacheperiod(31556926); registry.addresourcehandler("/extras/**").addresourcelocations("/extras/").setcacheperiod(31556926); registry.addresourcehandler("/jquery/**").addresourcelocations("/jquery/").setcacheperiod(31556926); } @override public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { configurer.enable(); } }
dispatcherconfig.java
@controller @import(webappconfig.class) public class dispatcherconfig { @bean public viewresolver jspresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setprefix("/web-inf/jsp/"); viewresolver.setsuffix(".jsp"); return viewresolver; } }
i want add others dispatcher servlet application. first idea ass following code classes above:
in webappinitializer.java
a new block this, changing names in proper places:
// create dispatcher servlet's spring application context annotationconfigwebapplicationcontext dispatchercontext = new annotationconfigwebapplicationcontext(); dispatchercontext.register(dispatcherconfig.class); // register , map dispatcher servlet servletregistration.dynamic dispatcher = container.addservlet("dispatcher", new dispatcherservlet(dispatchercontext)); dispatcher.setloadonstartup(1); dispatcher.addmapping("/");
and add new class dispatcherconfig.java, name chosen in code above.
my questions are:
1) first of all, approach right way add new dispatcher servlet?
2) second, if answer question 1 'yes', names should change in webappinitializer?
3) in controller(s), how sinalize dispatcher servlet requisition should go? controllers use methods following call view:
@requestmapping(value="view_mapping") public method() { modelandview mav = new modelandview() mav.setviewname("view_name"); return mav; }
you can have many dispatcherservlets
want. need duplicate configuration , give servlet different name (else overwrite previous one), , have separate configuration classes (or xml files) it.
your controllers shouldn't care in dispatcherservlet
run neither should include code detect (what if add another, , need keep modifying controllers fix that).
however while can have multiple servlets in general there isn't need multiple servlets , can handle single instance of dispatcherservlet
.
Comments
Post a Comment