spring - component-scan get in the way of bean initialization? -
i encounter problem while trying duplicate simple spring oauth project, sparklr2. source code here https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2/sparklr
the source code runs perfectly, when debug tomcat, initialize @bean inside webmvcconfigureradapter, including controllers. noted @componentscan() not being used.
then create own mvc project, copy 100% of code, using webapplicationinitializer instead of abstractdispatcherservletinitializer. use webapllicationinitializer because have learned way code mvc.
then run project, @bean initialized. check /login browser, 404. caused spring not knowing have controllers, add @componentscan configuration class, /login shows up. weird thing is, @bean related controller, not initialized. so, when call method controller, since attributes not initialized, gives me no object or null exception.
so, point is, how sample works, mean controller , jsp correctly handle , response without using @componentscan? , @ different angle, why @componentscan stop @bean being initialize in project?
my webapplicationinitializer
@configuration @enablewebmvc @componentscan("umedia.test.oauth.controller") public class mvcconfig extends webmvcconfigureradapter { @bean public static propertysourcesplaceholderconfigurer propertysourcesplaceholderconfigurer() { return new propertysourcesplaceholderconfigurer(); } @bean public contentnegotiatingviewresolver contentviewresolver() throws exception { contentnegotiationmanagerfactorybean contentnegotiationmanager = new contentnegotiationmanagerfactorybean(); contentnegotiationmanager.addmediatype("json", mediatype.application_json); internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); mappingjackson2jsonview defaultview = new mappingjackson2jsonview(); defaultview.setextractvaluefromsinglekeymodel(true); contentnegotiatingviewresolver contentviewresolver = new contentnegotiatingviewresolver(); contentviewresolver .setcontentnegotiationmanager(contentnegotiationmanager .getobject()); contentviewresolver.setviewresolvers(arrays .<viewresolver> aslist(viewresolver)); contentviewresolver.setdefaultviews(arrays.<view> aslist(defaultview)); return contentviewresolver; } @bean public photoserviceimpl photoservices() { list<photoinfo> photos = new arraylist<photoinfo>(); photos.add(createphoto("1", "marissa")); photos.add(createphoto("2", "paul")); photos.add(createphoto("3", "marissa")); photos.add(createphoto("4", "paul")); photos.add(createphoto("5", "marissa")); photos.add(createphoto("6", "paul")); photoserviceimpl photoservices = new photoserviceimpl(); photoservices.setphotos(photos); return photoservices; } // n.b. @qualifier here should not necessary (gh-298) lots of // users report needing it. @bean public admincontroller admincontroller( tokenstore tokenstore, @qualifier("consumertokenservices") consumertokenservices tokenservices, sparklruserapprovalhandler userapprovalhandler) { admincontroller admincontroller = new admincontroller(); admincontroller.settokenstore(tokenstore); admincontroller.settokenservices(tokenservices); admincontroller.setuserapprovalhandler(userapprovalhandler); return admincontroller; } // url, need change it? private photoinfo createphoto(string id, string userid) { photoinfo photo = new photoinfo(); photo.setid(id); photo.setname("photo" + id + ".jpg"); photo.setuserid(userid); photo.setresourceurl("/org/springframework/security/oauth/examples/sparklr/impl/resources/" + photo.getname()); return photo; } @override public void configuredefaultservlethandling( defaultservlethandlerconfigurer configurer) { configurer.enable(); } @bean public photoserviceusercontroller photoserviceusercontroller( photoservice photoservice) { photoserviceusercontroller photoserviceusercontroller = new photoserviceusercontroller(); return photoserviceusercontroller; } @bean public photocontroller photocontroller(photoservice photoservice) { photocontroller photocontroller = new photocontroller(); photocontroller.setphotoservice(photoservice); return photocontroller; } @bean public accessconfirmationcontroller accessconfirmationcontroller( clientdetailsservice clientdetailsservice, approvalstore approvalstore) { accessconfirmationcontroller accessconfirmationcontroller = new accessconfirmationcontroller(); accessconfirmationcontroller .setclientdetailsservice(clientdetailsservice); accessconfirmationcontroller.setapprovalstore(approvalstore); return accessconfirmationcontroller; } /* @bean public viewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setviewclass(jstlview.class); viewresolver.setprefix("/web-inf/views/"); viewresolver.setsuffix(".jsp"); return viewresolver; }*/ @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/resources/**").addresourcelocations( "/resources/"); } }
so, have @componentscan
interacts @controller
on controllers + still create @bean
's those?
as first step try remove @beans
, try inject dependencies using @autowired
on controllers' constructors. @componentscan should recognize @controller
, inject dependencies , use @requestmapping
without issues.
Comments
Post a Comment