无法使用java注释(JSR330标准注释)跨TestNG的各种类使用spring依赖项注入

无法使用java注释(JSR330标准注释)跨TestNG的各种类使用spring依赖项注入,java,spring,dependency-injection,jsr330,Java,Spring,Dependency Injection,Jsr330,我有一个服务,我正试图在我的测试中跨不同的类注入它,但我得到它的实例为null 我的配置界面: MyService.java public interface MyService { public String getHostUri(); } 这个接口的我的实现类:MyServiceImpl.java 我的带有bean的Spring配置类: @Configuration public class AutomationSpringConfig { @Bean p

我有一个服务,我正试图在我的测试中跨不同的类注入它,但我得到它的实例为null

我的配置界面:

MyService.java

public interface MyService {

    public String getHostUri();

}
这个接口的我的实现类:MyServiceImpl.java

我的带有bean的Spring配置类:

@Configuration

public class AutomationSpringConfig {

    @Bean
    public MyService getMyService(){
        return new MyServiceImpl();
    }

}
我的testNG类:

@ContextConfiguration(classes=AutomationSpringConfig.class ,loader =AnnotationConfigContextLoader.class)

public class BasicAutomatedTest extends AbstractTestNGSpringContextTests {

    private static final Logger LOGGER = Logger.getLogger(BasicAutomatedTest.class);

    @Inject
    private MyService myService;


    @Test
    public void basicTest {
        Setup setup = new Setup();
        LOGGER.info(myService.getHostUri());
        LOGGER.info(setup.myService.getHostUri());

    }
}
我无法获得注射的助手类:

public class Setup {

  @Inject

  public MyService myService;

}
因此,当我试图通过BasicAutomatedTest的basicTest方法中的setup对象获取hostUri时,我会得到一个NullPointerException。
因此,我无法在Setup类中注入MyService bean。

为了使用注释,您需要在beans XML配置文件中指定该行为。大概是这样的:

<context:component-scan base-package="your.base.package"/>
<context:annotation-config/>


希望有帮助

我只是使用基于Java的注释,而不是XML配置,所以您可以根据注释进行指定。
<context:component-scan base-package="your.base.package"/>
<context:annotation-config/>