EJB 3.1:单例bean未能在另一个无状态bean中注入,尽管两个bean都已注册。

5

下面是我的 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);
    }
}

这是 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;
    }

}

这是一个非常简单的JAX-RS实现,我将其部署为JBoss 6.1 Final中的war。问题在于当我进行适当的get请求时,InformationService会抛出NullPointerException。如果我显式初始化appService,一切都正常工作。为什么@EJB注释不起作用?

2个回答

0

请检查您的@Singleton是否为javax.ejb.Singleton。在NPE之前还有其他任何异常吗?


0

你正在使用Jersey作为REST实现吗?如果是的话,EJB注入不会被默认支持。

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


我正在使用JBoss的JAX-RS实现(而不是RestEasy) - Daud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接