AbstractTestNGSpringContextTests是否支持spring范围? 我正在尝试为我的类设置原型作用域,但是我不断从不同的线程中获取相同的实例(如“ 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)
答案 0 :(得分:0)
将ActiveProfiles与您的相关个人资料添加到TestConfig
@ActiveProfiles(profiles = { "testing" })
ActiveProfiles是一个类级别的批注,用于声明在为测试类加载ApplicationContext时应使用哪些活动bean定义配置文件。