用于单例DI的微微容器 [英] picocontainer for singleton DI

查看:98
本文介绍了用于单例DI的微微容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将picocontainer用于DI,但仍然使我的共享对象实例化了几次,而不是作为一个单例自动进行管理。这是一个例子来说明。类ASteps和BSteps通过其构造函数接收一个SharedObject实例。我希望它可以由picocontainer作为单例进行管理:根据Cucumber文档,仅实例化一次。相反,我看到它对ASteps实例化一次,对BSteps实例化一次:

I'm trying to use picocontainer for DI but still getting my shared object instantiated several times instead of being automatically managed as a singleton. Here's an example to illustrate. Classes ASteps and BSteps receive a SharedObject instance through their constructors. I expected it to be managed as a singleton by picocontainer: instantiated only once, as per the Cucumber docs. Instead, I see it gets instantiated once for ASteps and once for BSteps:

Running my.domain.CucumberRunTest
 INFO [main] (CucumberHooks.java:15) - Executing before()
 INFO [main] (SharedObject.java:11) - SharedObject - instantiated
 INFO [main] (ASteps.java:21) - Executing a_step_one()
 INFO [main] (ASteps.java:26) - Executing a_step_two()
 INFO [main] (ASteps.java:31) - Executing a_step_three()
 INFO [main] (CucumberHooks.java:20) - Executing after()
 INFO [main] (CucumberHooks.java:15) - Executing before()
 INFO [main] (SharedObject.java:11) - SharedObject - instantiated
 INFO [main] (BSteps.java:23) - Executing b_step_one()
 INFO [main] (BSteps.java:28) - Executing b_step_two()
 INFO [main] (BSteps.java:33) - Executing b_step_three()
 INFO [main] (CucumberHooks.java:20) - Executing after()

我在做什么错?这是代码:

What am I doing wrong? Here is the code:

package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class ASteps {

final Logger log = LoggerFactory.getLogger(getClass());
SharedObject sharedObject;

public ASteps(SharedObject sharedObject) {
this.sharedObject = sharedObject;
}

@Given("^A step one$")
public void a_step_one() {
log.info("Executing a_step_one()");
}

@When("^A step two$")
public void a_step_two() {
log.info("Executing a_step_two()");
}

@Then("^A step three$")
public void a_step_three() {
log.info("Executing a_step_three()");
}
}

************************

package my.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class BSteps {

final Logger log = LoggerFactory.getLogger(getClass());
SharedObject sharedObject;

public BSteps(SharedObject sharedObject) {
this.sharedObject = sharedObject;
}

@Given("^B step one$")
public void b_step_one() {
log.info("Executing b_step_one()");
}

@When("^B step two$")
public void b_step_two() {
log.info("Executing b_step_two()");
}

@Then("^B step three$")
public void b_step_three() {
log.info("Executing b_step_three()");
}
}

************************

package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.api.java.After;
import cucumber.api.java.Before;

public class CucumberHooks {

final Logger log = LoggerFactory.getLogger(getClass());

@Before
public void before() {
log.info("Executing before()");
}

@After
public void after() {
log.info("Executing after()");

}
}

*********************

package my.domain;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class CucumberRunTest {

}

**********************

package my.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SharedObject {

final Logger log = LoggerFactory.getLogger(getClass());

public SharedObject() {
log.info("SharedObject - instantiated");
}
}


推荐答案

pico容器的缓存为在场景之间重置时,场景结束时将其重置。

The cache of the pico container is reset between scenarios, when the world is disposed of at the end of the scenario.

每个设计,以避免状态在方案之间泄漏,并使它们隔离,从而使测试顺序不会影响结果。

This is very much per design to avoid state leaking between scenarios and make them isolated so the order of the tests doesn't influence the results.

如果您希望维护状态在方案A和方案B之间,您要么需要自己在pico容器之外处理 SharedObject ,要么可以使这两个方案之间的依赖关系显式化-例如,通过使用背景

If you do wish to maintain state between scenario A and scenario B, you either need to handle SharedObject yourself, outside of the pico container, or you can make the dependency between these two scenarios explicit – for example by using a Background.

这篇关于用于单例DI的微微容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆