components - Spring: Properly setup @ComponentScan -
i have following set spring application context
.
@configuration public class rmicontext { @bean public rmiproxyfactorybean service() { rmiproxyfactorybean rmiproxy = new rmiproxyfactorybean(); rmiproxy.setserviceurl("rmi://127.0.1.1:1099/service"); rmiproxy.setserviceinterface(service.class); return rmiproxy; } }
@configuration public class localcontext { @bean public controller controller() { return new controllerimpl(); } }
@configuration @import({rmicontext.class, localcontext.class}) public class maincontext { }
the above setup works fine, want enable @componentscan
annotating controller
s @component
there many controller
s in application tedious when declared 1 one using @bean
.
@configuration @componentscan(basepackageclasses = {controller.class}) public class localcontext { /* ... */ }
problem when @componentscan(basepackageclasses = {controller.class})
, fine working rmiproxyfactorybean
not recognized or can't created. so, how configure maincontext
both beans via rmi , local beans created?
@configuration candidate component scan, can scan beans in rmicontext , controllers in controller package by:
@configuration @componentscan(basepackages = {"org.example.controllers", "package.of.rmicontext"}) public class maincontext { }
--edit--
@configuration candidate component scan, here test case works in pc:
package scan.controllers; @controller public class examplecontroller { } package scan; public interface rmiservice { } package scan; @configuration public class rmicontext { @bean public rmiproxyfactorybean service() { rmiproxyfactorybean rmiproxy = new rmiproxyfactorybean(); rmiproxy.setserviceurl("rmi://127.0.1.1:1099/service"); rmiproxy.setserviceinterface(rmiservice.class); rmiproxy.setlookupstubonstartup(false); return rmiproxy; } } package scan; @configuration //maincontext auto scan rmicontext in package scan , controllers in package scan.controllers @componentscan(basepackages = {"scan", "scan.controllers"}) public class maincontext { } package scan; @runwith(springjunit4classrunner.class) @contextconfiguration(classes={maincontext.class}) public class testcontext { @autowired private rmiservice rmi; @autowired private examplecontroller controller; @test public void test() { //both controller , rmi service autowired expected assertnotnull(controller); assertnotnull(rmi); } }
Comments
Post a Comment