0

I am very new to developing enterprise applications using Java EE. I have been using jdeveloper for this. Although I have gone through some books, I am still finding it difficult to understand the practical use and benefit of some modules.

It would be great if you anybody could answer some questions (written below the code) regarding annotations.

import javax.annotation.Resource;

import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless(name = "StudentWSApiBean", mappedName = "RizwanWS-RizwanService-StudentWSApiBean")
@WebService(name = "StudentWSApi", serviceName = "StudentWSApi", portName = "StudentWSApiPort")
public class StudentWSApiBeanBean implements StudentWSApiBean {
    @Resource
    SessionContext sessionContext;

    @EJB
    StudentSession mySession;

    public StudentWSApiBeanBean() 
    {
    }

    @WebMethod
    public StudentResponse saveStudentInfo(@WebParam(name = "arg0")
        StudentRequest rqst)
    {
        StudentResponse resp = new StudentResponse();
        resp.setStat(0);
        try
        {
            int ret = mySession.saveStudentInfo(rqst.getName(), rqst.getAddr(), rqst.getClass_(), rqst.getGrade());        
            resp.setStat(ret);
        }catch(Exception exc)
        {
        }
        return resp;
    }
}

In the above code,

@EJB
StudentSession mySession;

Then I use,

mySession.saveStudentInfo(rqst.getName(), rqst.getAddr(), rqst.getClass_(), rqst.getGrade()); 

i.e. i am using mySession to access a method of StudentSession. Now, what benefit is the @EJB annotations doing here is not clear to me.

@Resource
SessionContext sessionContext;

I haven't seen the use of sessionContext anywhere in the code except the declaration. And what does the @Resource meaning here.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • SessionContext really is superflous since it is not used. As for the EJB annotations, there is plenty of reference material on the net. If you have looked it up and have questions about the details, post them here, but SO is not a personal research assistant. – kostja Sep 12 '12 at 09:01

2 Answers2

0

You've been correctly directed to read some Java EE tutorials.

If you need a name for the @Resources construct to search for, you could type "Java EE resource injection", or "Java EE dependency injection". No point of answering it directly, since there is a plenty of reading everywhere.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
MaDa
  • 10,511
  • 9
  • 46
  • 84
0

In Java EE 5 you can use injection statement like @EJB. This simplifies the development process and avoid the lookup.

Before Java EE 5 if you want to use an EJB you have first make a lookup to get the remote/local interface and then use it. In Java EE 5 you can simply declare @EJB and the container inject into your EJB the reference and you can use it directly.

The same thing is @Resources.

As said there are plenty of books and article expalining injection and its benefits.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22