AbstractTestNGSpringContextTests具有原型范围



AbstractTestNGspringContextTest支持弹簧作用域吗?我试图为我的类设置一个原型范围,但我一直从不同的线程中获得相同的实例(作为"singleton"(

我的配置类:

@Configuration
@ComponentScan
@PropertySource("classpath:/sut/${env}")
public class TestConfig {
@Autowired
private Environment env;
@Bean
public ZookeeperDriver zookeeper() throws Exception {
return new ZookeeperDriver(env.getProperty("zookeeper.host"), env.getProperty("zookeeper.internalIp"));
}
@Bean
public UserAgent userAgent() throws Exception {
return new UserAgent(env.getProperty("userAgent.europeAgentIp"), 
env.getProperty("userAgent.europeAgentIp2"), 
env.getProperty("userAgent.apacAgentIp"),
env.getProperty("userAgent.useastAgentIp"), 
env.getProperty("userAgent.uswestAgentIp"),
env.getProperty("userAgent.stabilityAgentIp"), 
env.getProperty("userAgent.parisAgentIp"));
}
@Bean
public IpSecUserAgent ipSecUserAgent() throws Exception {
return new IpSecUserAgent(env.getProperty("ipSecUserAgent.amsAgentIp"), env.getProperty("ipSecUserAgent.amsAgentIntIp"),
env.getProperty("ipSecUserAgent.svAgentIp"), env.getProperty("ipSecUserAgent.svAgentIntIp"));
}
@Bean()
public IpSecGateway ipSecGateway() throws Exception {
return new IpSecGateway(env.getProperty("ipSecGateway.amsGatewayIp"),
env.getProperty("ipSecGateway.amsGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp"),
env.getProperty("ipSecGateway.svGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp2"),
env.getProperty("ipSecGateway.svGatewayIntMask2"));
}
}

我的班级

@Component
@Scope("prototype")
public class IpSecGateway extends UserAgent {
private GatewayHost amsGw;
private GatewayHost svGw;
private GatewayHost svGw2;
private List<GatewayHost> gatewayHosts = new ArrayList<>();
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public IpSecGateway(String amsGatewayIp, String amsGatewayIntMask,
String svGatewayIp, String svGatewayIntMask, String svGatewayIp2,
String svGatewayIntMask2 ) throws Exception {
super("");
this.amsGw = new GatewayHost("ams", amsGatewayIp, amsGatewayIntMask);
this.svGw = new GatewayHost("sv", svGatewayIp, svGatewayIntMask);
this.svGw2 = new GatewayHost("sv2", svGatewayIp2, svGatewayIntMask2);
gatewayHosts.add(amsGw);
gatewayHosts.add(svGw);
gatewayHosts.add(svGw2);
}
public GatewayHost getAmsGw() {
return amsGw;
}
public void setAmsGw(GatewayHost amsGw) {
this.amsGw = amsGw;
}
public GatewayHost getSvGw() {
return svGw;
}
public void setSvGw(GatewayHost svGw) {
this.svGw = svGw;
}
public GatewayHost getSvGw2() {
return svGw2;
}
public void setSvGw2(GatewayHost svGw2) {
this.svGw2 = svGw2;
}
public List<GatewayHost> getGatewayHosts() {
return gatewayHosts;
}
public void setGatewayHosts(List<GatewayHost> gatewayHosts) {
this.gatewayHosts = gatewayHosts;
}
}

基类注入:

@ContextConfiguration(classes = TestConfig.class)
@Listeners({HtmlReporter.class, SlackDriver.class})
public class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
protected IpSecUserAgent ipSecUserAgent;
@Autowired
protected IpSecGateway ipSecGateway;
}

当我尝试从不同的线程调用ipsecGateway实例时,我每次都会得到相同的实例(例如getMessage(

使用相关配置文件将ActiveProfiles添加到TestConfig

@ActiveProfiles(profiles = { "testing" })

ActiveProfiles是一个类级注释,用于声明在为测试类加载ApplicationContext时应使用哪些活动bean定义概要文件。

最新更新