Is it bad practice to apply <context:component-scan> to every package in a Spring MVC (3.0) app?
Is it ok to do this since it will include all packages and subpackages in the app for component scanning?
<context:annotation-config />
<context:component-scan b开发者_如何学运维ase-package="rootpackage" />
Or is there a reason to be selective about which packages to scan?
e.g.
<context:annotation-config />
<context:component-scan base-package="rootpackage.abc" />
<context:component-scan base-package="rootpackage.def" />
<!--<context:component-scan base-package="rootpackage.ghi" /> --> Omit? Why?
Why would you ever omit packages from the scan?
Yes, it's bad practice.
If components can appear in any package, readers of your sourcebase need to search every class for Spring annotations, instead of just looking in, say, a "components" package and a "controllers" package.
Plus, you shouldn't have Spring components scattered around your package structure. Group your classes into meaningful packages and only expose those.
It make sense to be selective if you have several configs. For instance dispatcher-servlet.xml
, applicationContext-common-business.xml
, applicationContext-security.xml
etc
精彩评论