java - Spring @Autowired 字段为空。我在不同的类中使用@AutoWired Fields

标签 java spring autowired

这是我的类代码,其中有 @Autowired 字段:

测试A

@ContextConfiguration("classpath:spring.xml")
public abstract class TestA extends AbstractTestNGSpringContextTests {

    @Autowired
    @Value("#{environment.username}")
    public String username;

    @Autowired
    @Value("#{environment.password}")
    public String password;

    @Autowired
    @Value("#{environment.passwordtoken}")
    public String passwordToken;
}

这是我尝试使用上述字符串变量的另一个类

public class TestData extends TestA {

    ForceApi api;


    public void setApi() {
        // Instantiating api by setting username and pwd
        System.out.println("In getApi");
        ApiConfig ac = new ApiConfig();
        ac.setUsername(username);
        ac.setPassword(passwordToken);             ac.setLoginEndpoint("https://login.salesforce.com/services/Soap/u/34.0/");

        api = new ForceApi(ac);
    }

    public void pTestData() {

        setApi();
        // Create test data for play group admin
        String idSoql = "SELECT id FROM PlayGroup__c";

        List<Map> mapResults = api.query(idSoql).getRecords();
    }
}

并尝试使用以下类执行测试:

public class TestB extends TestA{
    @Test(dataProvider="browsers")
    public void test1(){
        TestData td = new TestData();
        td.pTestData();
    }

}

请帮我找出哪里做错了。我在这一行中遇到空指针异常:

List<Map> mapResults = api.query(idSoql).getRecords();

最佳答案

我自己解决了上述问题。

我在使用 @Autowired 字段的类中使用了 @Service("testdata")

@Service("testdata")
public class TestData extends TestA {

ForceApi api;


public void setApi() {
    // Instantiating api by setting username and pwd
    System.out.println("In getApi");
    ApiConfig ac = new ApiConfig();
    ac.setUsername(username);
    ac.setPassword(passwordToken);             ac.setLoginEndpoint("https://login.salesforce.com/services/Soap/u/34.0/");

    api = new ForceApi(ac);
}

public void pTestData() {

    setApi();
    // Create test data for play group admin
    String idSoql = "SELECT id FROM PlayGroup__c";

    List<Map> mapResults = api.query(idSoql).getRecords();
}

}

在 TestB 中更改如下

public class TestB extends TestA{
@Test(dataProvider="browsers")
public void test1(){
    TestData td = (TestData )applicationContext.getBean("testdata");
    td.pTestData();
}

}

并在 spring.xml 中添加如下 bean

<bean id="testdata" class="data.TestData"/> 

这解决了我的空指针异常问题。

关于java - Spring @Autowired 字段为空。我在不同的类中使用@AutoWired Fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34449731/

相关文章:

java - Singleton "getInstance"函数的 Unresolved Java 编译问题

java - 检查并避免博客 android 应用程序上的显式内容

java - 在 Spring mongoTemplate 中编写类似的等效查询..或者我应该使用什么?

Java 内存模型 : Is it safe to create a cyclical reference graph of final instance fields, 全部在同一个线程中分配?

java - 在 Libgdx 中使用 SQLite 数据库

java - 有没有办法根据 Activity 配置文件不启动 spring boot 应用程序?

java - 在 hibernate 和 MySql 中通过外键链接的外部对象

java - 使用 Mockito 和 Junit 时如何自动连接 spring bean?

Spring @Autowired 混淆(容器或 session )

java - Springboot在抽象类中 Autowiring 新对象