依赖注入@EJB不能在@WebService中工作

时间:2016-12-13 08:32:52

标签: web-services jboss ejb

我正在尝试在WebService中注入DAO对象

@Stateless
public class MyDAOImpl implements MyDAO {...}

@Local
public interface MyDAO {...}

在网络服务中我想注入这个DAO:

@Stateless
@WebService(endpointInterface = "...")
public class PropertyDetailsWebServiceImpl implements PropertyDetailsWebService {
...
@EJB
MyDAO myDAO; // null pointer
}

当我尝试JNDI时 - 这有效:

    @Stateless
    @WebService(endpointInterface = "...")
    public class PropertyDetailsWebServiceImpl implements PropertyDetailsWebService {
    ...

        InitialContext ic = new InitialContext();
        loggerInstance = (MyDAO ) ic.lookup("java:global/ben/MyDAO Impl!pl.MyDAO");  //this works
    }

为什么@EJB注射不起作用?

1 个答案:

答案 0 :(得分:0)

我将maven依赖模块webservice从jar更改为ejb,这有效:D

改变
<dependency>
    <groupId>pl.xxx.webservice</groupId>
    <artifactId>webservice</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>jar</type>
</dependency>

<dependency>
    <groupId>pl.xxx.webservice</groupId>
    <artifactId>webservice</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>ejb</type> // this 
</dependency>