下面是我的 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注释不起作用?