繁体   English   中英

Spring @Value读取的继承属性为null

[英]Inherited properties read by Spring @Value are null

我试图在子类中使用spring通过@Value读取的一些属性。 这些属性在父类中正确读取,但在子类中最终为null。 我正在使用注释配置而不是xml配置。

例如,我尝试在TestListeners中使用WebTemplate的属性reportType来扩展WebTemplate:

@ContextConfiguration(classes = AppConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class WebTemplate extends AbstractTestNGSpringContextTests {

    @Autowired
    protected SeleniumDriver driver;

    @Value("${environment.url}")
    protected String environmentUrl;

    @Value("${report.type}")
    protected String reportType;

    @Value("${auto.open.report}")
    protected String autoOpenReport;}

在这里,属性最终为null。 如果我在WebTemplate中使用它们,则一切正常且正常。

public class TestListener extends WebTemplate implements ITestListener {    
    @Override
    public void onTestStart(ITestResult result) {
                System.out.println(reportType);
                System.out.println(autoOpenReport);

我还考虑了Webtemplate Object在测试周期中的某个时候被破坏,并试图在每次从super调用它们的测试开始时将它们存储在子类中。 这也不起作用:

public class TestListener extends WebTemplate implements ITestListener {

    private String reportType;
    private String autoOpenReport;

    @Override
    public void onTestStart(ITestResult result) {

        this.reportType = super.reportType;
        this.autoOpenReport = super.autoOpenReport;

有什么建议吗?

更新:经过调查后发现问题实际上是由弹簧引起的。 WebTemplate类用@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)注释。 据我了解,这会在每次测试之后和进入监听器之前立即破坏整个弹簧环境。 因此,当进入各自的testNg侦听器时,spring管理的所有内容都消失了。

我的兴趣是在测试类中仅自动连接PageObjects,因此我最终从Spring环境中删除了不是PageObject的所有内容。

您有2个类,每个类都扩展了WebTemplate但是其中一个不是由Spring管理的,因此不会被注入/处理。 扩展由Spring管理的类的事实并不会自动使创建的实例由Spring管理。

您有2个类,因此有2个不同的对象,并且它们不共享相同的WebTemplate属性。 它们是2个不同的类,因此是实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM