EJB注入为空

时间:2015-04-07 12:03:03

标签: java ejb

我编写了EJB3业务逻辑并在Glassfish下部署。 我现在要通过同一容器中的客户端测试它,注入@EJB接口。

问题是此界面 null

客户

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();
    }
    }
}

本地接口

public interface RepAlertManager {

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

THE STATELESS 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是构造函数中的无状态bean设置连接,并通过get()方法释放它

1 个答案:

答案 0 :(得分:1)

如果您使用的是Java EE 6,那么这可能会有所帮助
Testing an EJB with JUnit

注射时也不要使用static。 CDI无法注入静态变量。

相关问题