简体   繁体   English

EJB无状态会话Bean为空

[英]EJB Stateless session bean is null

I'm trying to access an EJB Stateless session bean from a local service. 我正在尝试从本地服务访问EJB无状态会话bean。 But when I call a method that is located on the bean, I get a NPE because the stateless bean is null. 但是,当我调用位于Bean上的方法时,会得到一个NPE,因为无状态Bean为空。

Here is the code: 这是代码:

The sateless bean: 无味豆:

@Startup
@Stateless(name = "LoginBean")
@LocalBean
public class LoginBean {


    public List<Long> getItemsForClient(String clientId, Long itemId) {
        System.out.println("clientID: " + clientId);
        System.out.println("itemID: " + itemId);

        List<Long> ret = new ArrayList<Long>();
        ret.add((long) 123456);
        ret.add((long) 123457);
        ret.add((long) 123458);
        ret.add((long) 123459);
        return ret;

    }

    }

The service: 服务:

@Stateless
@Path("/ctofservice")
public class CtoFService {

    @EJB
    LoginBean loginBean;

    public CtoFService() {

    }

    @GET
    @Produces("text/plain")
    @Path("test")
    public String convertCtoF() {

        Long l = (long) 123456;
        List<Long> servicesForClient = loginBean.getItemsForClient("cliID", l);
        return itemsForClient.toString();


    }

And the ApplicationConfig: 和ApplicationConfig:

@ApplicationPath("/")
public class ApplicationConfig extends Application {

    @SuppressWarnings("unchecked")
    @Override
    public Set<Class<?>> getClasses() {

        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(CtoFService.class);    
    }
}

I've been trying for a while and looking for possible solutions, but nothing came up. 我已经尝试了一段时间,正在寻找可能的解决方案,但是没有任何反应。

I'm using JBoss AS 7.1 and RESTEasy that cames with it. 我正在使用JBoss AS 7.1和它附带的RESTEasy。

When the bean should get instantiated? 何时应实例化bean?

Thanks. 谢谢。

I solved it by adding beans.xml file, It wasn´t present at the moment that I created the project, and I came across to that file searching for a solution after hours. 我通过添加beans.xml文件解决了这个问题,在我创建项目的那一刻不存在该文件,然后我发现该文件在几个小时后开始寻找解决方案。

So I placed the file in WEB-INF directory 所以我把文件放在WEB-INF目录中

The file contains: 该文件包含:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

And the problem is solved, I can access the beans through the webService. 问题解决了,我可以通过webService访问bean。

Thanks for trying to help. 感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM