简体   繁体   English

EJB注入为空

[英]EJB injection is null

I've written an EJB3 business logic and deployed under Glassfish. 我已经编写了EJB3业务逻辑并部署在Glassfish下。 I'm now going to test it through a client in same container, injecting interface with @EJB. 我现在将通过一个客户端在同一容器中对其进行测试,并使用@EJB注入接口。

The problem is that this interface is null 问题是此接口为

THE CLIENT 客户端

public class EjbTest {

@EJB
private static RepAlertManager rep;


    public static void main(String[] args) throws IOException, SQLException {

    try 
    {
        DBConnection.setTypeConnetionLocal(true);

        List<Long> result = rep.doSomething("ENTRATE");

        for(Long temp:result){
            System.out.println(temp);
        }

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    }
}

THE LOCAL INTERFACE 本地接口

public interface RepAlertManager {

     public List<Long> doSomething(String message) throws SQLException; }

THE STATELESS bean extending data source 无状态Bean扩展数据源

@Stateless public class RepAlertManagerImpl extends DataSource implements RepAlertManager {

public RepAlertManagerImpl() throws IOException {
    super();
}


public List<Long> doSomething(String message) throws SQLException {

        // some code
} }

DATA SOURCE bean is a stateless bean setting connection in the constructor and releasing it through a get() method DATA SOURCE bean是构造函数中的无状态bean设置连接,并通过get()方法释放它

This one might help, provided you're using Java EE 6 如果您使用的是Java EE 6,这可能会有所帮助
Testing an EJB with JUnit 使用JUnit测试EJB

Also don't use static when injecting. 注入时也不要使用static CDI can't inject a static var. CDI无法注入静态变量。

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

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