Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Spring强制我使用静态变量?_Java_Spring_Annotations_Testng_Static Members - Fatal编程技术网

Java 为什么Spring强制我使用静态变量?

Java 为什么Spring强制我使用静态变量?,java,spring,annotations,testng,static-members,Java,Spring,Annotations,Testng,Static Members,我仍然是春天的初学者。我有一个测试类如下运行的 所以,我不知道为什么myService.doSomething()生成NullPointerException?有人能给我建议我需要做什么吗?什么问题可能导致此错误?我怎么了?现在我用的是 @Service("springTest") public class SpringTest { private static MyService myService; @Autowired public void setMyService(MyS

我仍然是春天的初学者。我有一个测试类如下运行的

所以,我不知道为什么
myService.doSomething()生成NullPointerException?有人能给我建议我需要做什么吗?什么问题可能导致此错误?我怎么了?现在我用的是

@Service("springTest")
public class SpringTest {
  private static MyService myService;

  @Autowired
  public void setMyService(MyService myService) {
    SpringTest.myService = myService;
    System.out.println(myService.getClass());
  }
  //org.testng.annotations.Test
  @Test
  public void doSomethingTest() {
    load();
    // fine , without error
    myService.doSomething();
  }

  private void load(){
  // here codes for load spring configuration files
  }
}
PS:我想我不需要显示我的spring配置文件,因为它非常简单,我相信这不会导致任何错误。所以,我留下来描述。但是如果你想看,我可以给你看。然后,请假设我的spring配置文件已正确加载,并且该文件是在我的测试类运行之前加载的。 谢谢你阅读我的问题。顺便说一下,我也不能用field 注入和我只能使用setter方法注入


只需删除静态初始值设定项。保持塞特注射(我更喜欢)

使用第一个示例,第二个代码是错误的-不要使用它

确保测试是由spring运行的(因此,bean是由spring正确初始化的)

null指针是在spring初始化bean之前运行测试方法造成的

你需要这样的东西

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath:/config/applicationContext-test.xml")
public class SpringTest {...}

只需删除静态初始值设定项。保持塞特注射(我更喜欢)

使用第一个示例,第二个代码是错误的-不要使用它

确保测试是由spring运行的(因此,bean是由spring正确初始化的)

null指针是在spring初始化bean之前运行测试方法造成的

你需要这样的东西

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath:/config/applicationContext-test.xml")
public class SpringTest {...}

只需删除静态初始值设定项。保持塞特注射(我更喜欢)

使用第一个示例,第二个代码是错误的-不要使用它

确保测试是由spring运行的(因此,bean是由spring正确初始化的)

null指针是在spring初始化bean之前运行测试方法造成的

你需要这样的东西

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath:/config/applicationContext-test.xml")
public class SpringTest {...}

只需删除静态初始值设定项。保持塞特注射(我更喜欢)

使用第一个示例,第二个代码是错误的-不要使用它

确保测试是由spring运行的(因此,bean是由spring正确初始化的)

null指针是在spring初始化bean之前运行测试方法造成的

你需要这样的东西

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath:/config/applicationContext-test.xml")
public class SpringTest {...}


@Test是否来自jUnit?您正在尝试测试MyService吗?@geo和no,org.testng.annotations.test;
@Test是否来自jUnit?您正在尝试测试MyService吗?@geo和no,org.testng.annotations.test;
@Test是否来自jUnit?您正在尝试测试MyService吗?@geo和no,org.testng.annotations.test;
@Test是否来自jUnit?您正在尝试测试MyService吗?@geo和no,org.testng.annotations.test;当前我的第二个代码对我来说很好,我的第一个代码产生了我描述的错误。是的,你说得对,我是作为测试班运行的。我使用org.testng.annotations.Test并由testng运行。如果是,我该如何解决?谢谢。最后,一切都很好,先生。谢谢但我试图将我的spring配置文件加载为
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(“classpath:spring-core/spring-application-context.xml”)。但它无法修复。我真的很感谢你的回答,谢谢。现在我也可以使用场注入。我没想到那个测试库会伤到我的头。我注意到在另一个类中,文件是可以的,但是为什么我的测试类会产生这样的错误。。但现在一切都好了当前我的第二个代码对我来说很好,我的第一个代码产生了我描述的错误。是的,你说得对,我是作为测试班运行的。我使用org.testng.annotations.Test并由testng运行。如果是,我该如何解决?谢谢。最后,一切都很好,先生。谢谢但我试图将我的spring配置文件加载为
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(“classpath:spring-core/spring-application-context.xml”)。但它无法修复。我真的很感谢你的回答,谢谢。现在我也可以使用场注入。我没想到那个测试库会伤到我的头。我注意到在另一个类中,文件是可以的,但是为什么我的测试类会产生这样的错误。。但现在一切都好了当前我的第二个代码对我来说很好,我的第一个代码产生了我描述的错误。是的,你说得对,我是作为测试班运行的。我使用org.testng.annotations.Test并由testng运行。如果是,我该如何解决?谢谢。最后,一切都很好,先生。谢谢但我试图将我的spring配置文件加载为
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(“classpath:spring-core/spring-application-context.xml”)。但它无法修复。我真的很感谢你的回答,谢谢。现在我也可以使用场注入。我没想到那个测试库会伤到我的头。我注意到在另一个类中,文件是可以的,但是为什么我的测试类会产生这样的错误。。但现在一切都好了当前我的第二个代码对我来说很好,我的第一个代码产生了我描述的错误。是的,你说得对,我是作为测试班运行的。我使用org.testng.annotations.Test并由testng运行。如果是,我该如何解决?谢谢。最后,一切都很好,先生。谢谢但我试图将我的spring配置文件加载为
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(“classpath:spring-core/spring-application-context.xml”)。但它无法修复。我真的很感谢你的回答,谢谢。现在我也可以使用场注入。我没想到那个测试库会伤到我的头。我注意到在另一个类中,文件是可以的,但是为什么我的测试类会产生这样的错误。。但现在一切都好了