我有这个课:

@RunWith(Arquillian.class)
public class myIT {
    @Inject
    private UserTransaction utx;

    @Resource(mappedName="java:/jdbc/myDS")
    private DataSource dataSource;

    @Deployment(name="DeployOne", order = 1)
    public static Archive<?> deployOne() throws FileNotFoundException {
        //Build and return the file
    }

    // @Deployment(name="DeployTwo", order = 2)
    public static Archive<?> deployTwo() throws FileNotFoundException {
        //Build an empty file return the file
        return = ShrinkWrap.create(WebArchive.class, "deployTwo.war");
    }

    @Before
    public void setup() throws Exception {
        utx.begin();
        //Do things with utx
    }
}


如您所见,对部署2进行了注释,因此我的部署工作正常。
如果取消注释@Deployment批注,则不会再注入utx,并且会出现空指针异常。

我想念什么吗?为什么添加新部署会导致不再注入我的UserTransaction?

最佳答案

您必须将UserTransaction作为资源注入,因为它是在CDI容器之外进行管理的。

private @Resource UserTransaction transaction;


参见此Tutorial

关于java - Arquillian无法通过多个部署注入(inject)UserTransaction,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33955189/

10-11 10:35