如何从不受容器管理的 class 访问容器管理对象
How to access a container managed object from a class that is not managed by the container
有没有办法从不是由容器创建或管理的 class 访问通常由容器创建和管理的对象?换句话说,代码可以在有容器时与 classes 的容器管理实例一起工作,而在没有容器时与常规 POJO 一起工作?
对我来说关键是测试 class 不知道它得到的 class 是来自容器还是一些存根。
我可能是守旧派,但我想使用可配置工厂 class 来访问我的数据库。在生产或集成测试中,它将 return 容器注入我的 DAL 接口的实现。进行单元测试时,我想去掉 DAL 和 return 静态值。
这可能吗?
如何做到?
这是我目前所处的位置,但仍然没有快乐...
工厂class
@Stateless
public class DALFactory
{
@Inject
private static DALInterface DAL;
public static void init(String fqcn)
{
// use reflect to create unit test instance
}
public static DALInterface getDAL()
{
return DAL;
}
}
测试class
public class test
{
public void testDALAccess()
{
Table t = new Table(); // Instance of class representing a table
DALFactory.getDAL().persist(t);
}
}
当我提供我的 testDAL 时,这工作得很好,因为我用 FQCN 调用了 init(),但当我尝试使用容器管理的实例时,它失败了。
您可能猜到我是 CDI 和 JPA 的新手。
仅供参考,我将 DALFactory
标记为 @Stateless
的唯一原因是当有容器时它可以由容器管理。
所以这就是我最终解决问题的方法。在我这样做之后,我能够隔离依赖于注入和相关容器的代码,这样我就可以在没有容器的情况下进行测试。
注意 - 我仍然对解决这个问题的其他方法持开放态度,这样我就可以在有和没有容器执行注入的情况下测试相同的代码。
// DAL工厂
public class DALFactory
{
private static DALInterface DAL;
public static void setDAL(DALInterface di)
{
DAL = di;
}
public static void init(String fqcn)
{
// use reflect to create unit test instance
}
public static DALInterface getDAL()
{
return DAL;
}
}
// 可注入根class,这必须是可注入树的顶部
@Stateless
public MainClass
{
@Inject
ProdDAL dal;
@PostConstruct
public void postConstruct()
{
DALFactory.setDAL(new DALWrapper(this.dal));
}
}
// DALWrapper
public DALWrapper implements DALInterface
{
private ProdDAL dal;
public DALWrapper(ProdDAL prodDAL)
{
this.dal = prodDal;
}
... rest of interface goes here
}
有没有办法从不是由容器创建或管理的 class 访问通常由容器创建和管理的对象?换句话说,代码可以在有容器时与 classes 的容器管理实例一起工作,而在没有容器时与常规 POJO 一起工作?
对我来说关键是测试 class 不知道它得到的 class 是来自容器还是一些存根。
我可能是守旧派,但我想使用可配置工厂 class 来访问我的数据库。在生产或集成测试中,它将 return 容器注入我的 DAL 接口的实现。进行单元测试时,我想去掉 DAL 和 return 静态值。
这可能吗?
如何做到?
这是我目前所处的位置,但仍然没有快乐...
工厂class
@Stateless
public class DALFactory
{
@Inject
private static DALInterface DAL;
public static void init(String fqcn)
{
// use reflect to create unit test instance
}
public static DALInterface getDAL()
{
return DAL;
}
}
测试class
public class test
{
public void testDALAccess()
{
Table t = new Table(); // Instance of class representing a table
DALFactory.getDAL().persist(t);
}
}
当我提供我的 testDAL 时,这工作得很好,因为我用 FQCN 调用了 init(),但当我尝试使用容器管理的实例时,它失败了。
您可能猜到我是 CDI 和 JPA 的新手。
仅供参考,我将 DALFactory
标记为 @Stateless
的唯一原因是当有容器时它可以由容器管理。
所以这就是我最终解决问题的方法。在我这样做之后,我能够隔离依赖于注入和相关容器的代码,这样我就可以在没有容器的情况下进行测试。
注意 - 我仍然对解决这个问题的其他方法持开放态度,这样我就可以在有和没有容器执行注入的情况下测试相同的代码。
// DAL工厂
public class DALFactory
{
private static DALInterface DAL;
public static void setDAL(DALInterface di)
{
DAL = di;
}
public static void init(String fqcn)
{
// use reflect to create unit test instance
}
public static DALInterface getDAL()
{
return DAL;
}
}
// 可注入根class,这必须是可注入树的顶部
@Stateless
public MainClass
{
@Inject
ProdDAL dal;
@PostConstruct
public void postConstruct()
{
DALFactory.setDAL(new DALWrapper(this.dal));
}
}
// DALWrapper
public DALWrapper implements DALInterface
{
private ProdDAL dal;
public DALWrapper(ProdDAL prodDAL)
{
this.dal = prodDal;
}
... rest of interface goes here
}