spring - @Inject , @AutoWired, @Resource & @ManagedProperty : which one should I use ,where and when? -
i'm using hibernate, spring & jsf application. i'm evolving application restfull webservice jersey(jax-rs). need annotated class @component. inside class, need call service grap stuffs database.
@component @path("/graphic") public class graphicservice { //@autowired //@inject //participantbo participantbo; or //@managedproperty("#{participantbo}") //private participantbo participantbo;
i meet annotations in tutorials don't know/understand meaning. i'd make check-up see if configuration whole application ok or if clean stuffs.
most of time, i'm using @managedproperty annotation include dependency (a servicebo 1 call dao) inside class annotated @managedbean.
@managedbean(name="participantcontroller") @viewscoped public class addparticipantbean implements serializable{ private static final long serialversionuid = -6952203235934881190l; @managedproperty(value="#{participantbo}") participantbo participantbo; }
i have applicationcontext.xml file declare classes way :
<!-- participant data access object --> <bean id="participantdao" class="x.x.x.dao.participant.participantdaoimpl" > <property name="sessionfactory" ref="sessionfactory"></property> </bean> <!-- participant business object --> <bean id="participantbo" class="x.x.x.bo.participant.participantboimpl" > <property name="participantdao" ref="participantdao" /> </bean>
is configuration done ? configure application differently ? , maybe without xml declaration ? using @inject or @autowired maybe ? use cases them ?
i prefere use standards java ee provides. , prefere annotate setters instead property directly. way more easy start doing unit testing (and mocking these objects). check answer here
for example class graphicservice this:
@component @path("/graphic") public class graphicservice { participantbo participantbo; @resource public void setparticipantbo(participantbo participantbo){ this.participantbo = participantbo; }
more info @resource
hope helps.
Comments
Post a Comment