java - Add packages to component-scan -
i have project spring container. let's project called my-common-library
, uses namespace my.common
. scans components in namespace, specified in common-context.xml
looks following
<beans ...> <context:component-scan base-package="my.common"/> </beans>
among other things scan detects classes annotated @mycomponent
.
now reuse project possible. let's start new project my-client
uses in namespace my.client
. my-client
consists of components annotated @mycomponent
.
in ideal world add dependency my-common-library
, @mycomponent
s scanned , registered. problem new namespace unknown original my-common-library
.
one solution i'm aware of add updated common-context.xml
my-client
like
<beans ...> <context:component-scan base-package="my.common,my.client"/> </beans>
that work, seems quite fragile. there more elegant solution maybe?
attempting implement component registration libraries via component-scan fragile, in opinion.
if reusing code, recommend explicitly importing my-common-library
dependency. example, using java-based spring configuration:
@configuration @componentscan("my.common") public class mycommonlibraryconfig { }
on 'my-client`:
@configuration @import(mycommonlibraryconfig.class) @componentscan("my.client") public class myclientconfig { }
since my-client
depends on my-library
, best define dependency explicitly.
on other hand, if want implement plugin system, need use package-based convention discover dependencies since component using dependencies not know dependencies until run-time.
in case, recommend defining registration package name such my.plugin
, spring configuration component depends on plugins need define component scan on my.plugin
, , each plugin need define @components
or @configurations
beans in same my.plugin
package.
if want more control, can add filter component-scan register bean annotation. example, assuming define @myplugin
annotation:
@componentscan( basepackages = {"my.plugin"}, includefilters = @componentscan.filter( value= myplugin.class, type = filtertype.annotation ) )
Comments
Post a Comment