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.

4

2 回答 2

0

在 Java EE 5 中,您可以使用 @EJB 之类的注入语句。这简化了开发过程并避免了查找。

在 Java EE 5 之前,如果您想使用 EJB,您必须首先进行查找以获取远程/本地接口,然后使用它。在 Java EE 5 中,您可以简单地声明 @EJB 并且容器将引用注入到您的 EJB 中,您可以直接使用它。

同样的事情是@Resources。

如前所述,有很多解释注射及其好处的书籍和文章。

于 2012-09-14T13:00:39.317 回答
0

已正确指导您阅读一些 Java EE 教程。

如果您需要一个@Resources要搜索的构造名称,您可以键入“Java EE 资源注入”或“Java EE 依赖注入”。没有必要直接回答,因为到处都有大量的阅读。

于 2012-09-12T09:11:01.450 回答