EJB 3.1:尽管两个bean都已注册,但是单例bean没有被注入到另一个无状态bean中

Dau*_*aud 5 singleton ejb-3.1

这是我的bean试图注入一个单例bean InformationService:

@Path("/information/{name}")
@Stateless (name="InformationResource")
public class InformationResource {

    @EJB
    private InformationService appService;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Information getInfo(@PathParam("name") String name){
        return appService.getMap().get(name);
    }

    @PUT
    @POST
    @Consumes(MediaType.APPLICATION_XML)
    public Information putInfo(@PathParam("name") String name, Information info){
        return appService.getMap().put(name,info);
    }

    @DELETE
    public void deleteInfo(@PathParam("name") String name){
        appService.getMap().remove(name);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是InformationService

@Singleton
public class InformationService {

    private Map<String,Information> map;

    @PostConstruct
    public void init(){
        map = new HashMap<String,Information>();

        map.put("daud", new Information("B.Tech","Lucknow"));
        map.put("anuragh", new Information("M.Sc","Delhi"));
    }

    public Map<String,Information> getMap(){
        return map;
    }

}
Run Code Online (Sandbox Code Playgroud)

它是一个非常简单的JAX-RS实现的一部分,我正在JBoss 6.1 Final中作为战争部署.问题是当我发出正确的get请求时,InformationService会抛出NullPointerException.如果我明确初始化appService,一切正常.为什么@EJB注释不起作用?

Gon*_*gui 0

您是否使用 Jersey 作为 REST 实现?如果是这样,则不支持开箱即用的 EJB 注入。

此链接提供了有关此问题的更多信息以及解决方案。