java - spring mvc annotation @Inject does not work -
i have following in app-servlet.xml
<mvc:annotation-driven /> <context:component-scan base-package="com.merc.myproject.web.controllers"/> <context:component-scan base-package="com.merc.myproject.web.forms"/>
what ever have in controller package gets injected same thing in forms package null.
my form looks this
public class selectdatesform { @inject iuserservice userservice; ..... }
my controllers looks this
@controller public class selectdates { @inject iuserservice userservice; ..... }
somebody please help
i guess selectdatesform
instantiated manually new
rather obtained spring context. in case not spring bean , therefore not subject dependency injection.
usually don't need inject dependencies manually created objects. if need so, have several options:
declare
selectdatesform
prototype-scoped bean , obtain fresh instance of spring context instead of creatingnew
:@component @scope("prototype") public class selectdatesform { ... }
and when need obtain new instance of it:
selectdatesform newform = applicationcontext.getbean(selectdatesform.class);
however, approach couples code spring's
applicationcontext
.if have no control on instantiation of
selectdatesform
(i.e. happens outside of code), can use@configurable
also can manually facilitate autowiring of object created
new
:selectdatesform newform = new selectdatesform(); applicationcontext.getautowirecapablebeanfactory().autowirebean(newform);
Comments
Post a Comment