-1

I'm using Hibernate, Spring & JSF for an application. I'm actually evolving the application with a Restfull WebService with Jersey(JAX-RS). For that need i annotated my class with @Component. Inside this class, i need to call a service to grap some stuffs from the database.

@Component
@Path("/Graphic")
public class GraphicService {

    //@Autowired //@Inject
    //ParticipantBo participantBo;

    or       

    //@ManagedProperty("#{participantBo}")
    //private ParticipantBo participantBo;

I meet some annotations in tutorials that i don't know/understand well the meaning. So i'd like to make a check-up to see if my configuration for the whole application is ok or if i could clean up some stuffs.

Most of the time, i'm using @ManagedProperty Annotation to include a dependency (a ServiceBO which one call then a Dao) inside my class annotated with @ManagedBean.

@ManagedBean(name="participantController")
@ViewScoped
public class AddParticipantBean  implements Serializable{


    private static final long serialVersionUID = -6952203235934881190L;

        @ManagedProperty(value="#{participantBo}")
        ParticipantBo participantBo;
}

I have an applicationContext.xml file where i declare all my classes this 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 my configuration well done ? Could i configure the application differently ? , maybe without xml declaration ? using @Inject or @AutoWired maybe ? But what are the use cases for them ?

ZheFrench
  • 1,164
  • 3
  • 22
  • 46

1 Answers1

0

I prefere to use the standards that Java EE provides. And also I prefere to annotate the setters instead the property directly. With this way is more easy to start doing unit testing (and mocking these objects). Check also my answer here

For example your class GraphicService will be like this:

@Component
@Path("/Graphic")
public class GraphicService {

   ParticipantBo participantBo;

   @Resource
   public void setParticipantBo(ParticipantBo participantBo){
        this.participantBo = participantBo;
   }

More info about @Resource

Hope it helps.

Community
  • 1
  • 1
Gerard Ribas
  • 717
  • 1
  • 9
  • 17