java - getServletConfigClasses() vs getRootConfigClasses() when extending AbstractAnnotationConfigDispatcherServletInitializer -
what difference between getservletconfigclasses()
vs getrootconfigclasses()
when extending abstractannotationconfigdispatcherservletinitializer
. i've been reading lot sources since morning haven't clear understanding on differences yet :
please have @ these 2 configurations :
1).
public class springmvcinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class[] { conservlet.class }; } @override protected class<?>[] getservletconfigclasses() { return null; } .... .... }
the conservlet.class
refering
@enablewebmvc @configuration @componentscan({ "com" }) @import({ securityconfig.class }) public class conservlet { @bean public internalresourceviewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/pages/"); viewresolver.setsuffix(".jsp"); return viewresolver; } }
2).
public class webinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return null; } @override protected class<?>[] getservletconfigclasses() { return new class<?>[] { webconfig.class }; } ..... }
the webconfig.class refering
@configuration @enablewebmvc @componentscan(basepackages = { "....." }) public class webconfig extends webmvcconfigureradapter { @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/resources/**").addresourcelocations("/resources/"); } @bean public viewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views"); viewresolver.setsuffix(".jsp"); return viewresolver; } }
i see both conservlet & webconfig (more or less) doing same things initializating view :
but why :
- conservlet returned in
getrootconfigclasses()
- while webconfig returned in
getservletconfigclasses()
i read documentation
both getrootconfigclasses() & getservletconfigclasses() for
specify @configuration and/or @component classes provided to.. (their differences )
- the root application context
getrootconfigclasses()
- the dispatcher servlet application context for
getservletconfigclasses()
but why conservlet & webconfig doing same things (like initizialising view), maybe i'm 1 misunderstood it. what's root context , dispatcher servlets (i know one) in simple term/example
thank you!
a bit on applicationcontext
hierarchies
spring's applicationcontext
provides capability of loading multiple (hierarchical) contexts, allowing each focused on 1 particular layer, such web layer of application or middle-tier services.
one of canonical examples of using hierarchical applicationcontext
when have multiple dispatcherservlet
s in web application , we're gonna share of common beans such datasources
between them. way, can define root applicationcontext
contain common beans , multiple webapplicationcontext
s inherit common beans root context.
in web mvc framework, each dispatcherservlet
has own webapplicationcontext
, inherits beans defined in root webapplicationcontext
. these inherited beans can overridden in servlet-specific scope, , can define new scope-specific beans local given servlet
instance.
typical context hierarchy in spring web mvc (spring documentation)
if you're living in single dispatherservlet
world, possible have 1 root context scenario:
single root context in spring web mvc (spring documentation)
talk cheap, show me code!
suppose we're gonna develop web application , we're going use spring mvc, spring security , spring data jpa. simple scenario, have @ least 3 different config files. webconfig
contains our web related configurations, such viewresolver
s, controller
s, argumentresolver
s, etc. following:
@enablewebmvc @configuration @componentscan(basepackages = "com.so.web") public class webconfig extends webmvcconfigureradapter { @bean public internalresourceviewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); return viewresolver; } @override public void configurepathmatch(pathmatchconfigurer configurer) { final boolean do_not_use_suffix_pattern_matching = false; configurer.setusesuffixpatternmatch(do_not_use_suffix_pattern_matching); } }
here i'm defining viewresolver
resolve plain old jsps, poor life decisions, basically. need repositoryconfig
, contains data access facilities such datasource
, entitymanagerfactory
, transactionmanager
, etc. following:
@configuration @enabletransactionmanagement @enablejparepositories(basepackages = "com.so.repository") public class repositoryconfig { @bean public datasource datasource() { ... } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { ... } @bean public platformtransactionmanager transactionmanager() { ... } }
and securityconfig
contains security related stuff!
@configuration @enablewebsecurity public class securityconfig extends websecurityconfigureradapter { @override @autowired protected void configure(authenticationmanagerbuilder auth) throws exception { ... } @override protected void configure(httpsecurity http) throws exception { ... } }
for gluing these together, have 2 options. first, can define typical hierarchical applicationcontext
, adding repositoryconfig
, securityconfig
in root context , webconfig
in child context:
public class servletinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class<?>[] { repositoryconfig.class, securityconfig.class }; } @override protected class<?>[] getservletconfigclasses() { return new class<?>[] { webconfig.class }; } @override protected string[] getservletmappings() { return new string[] { "/" }; } }
since have single dispatcherservlet
here, can add webconfig
root context , make servlet context empty:
public class servletinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class<?>[] { repositoryconfig.class, securityconfig.class, webconfig.class }; } @override protected class<?>[] getservletconfigclasses() { return null; } @override protected string[] getservletmappings() { return new string[] { "/" }; } }
further reading
skaffman did great job on explaining applicationcontext
hierarchies in answer, highly recommended. also, can read spring documentation.
Comments
Post a Comment