2

我正在创建一个 Spring MVC 应用程序。

控制器依赖于服务层中的服务之一[名称:TestExecutionOrchestratorService.java]。TestExecutionOrchestratorService 依赖于其他几个服务 [名称:JUnitTestExecutorService、QTPTestExecutorService]。配置被设置为在 Map 中注入这些服务。

Controller 和 TestExecutionOrchestratorService 使用注释自动装配。TestExecutionOrchestratorService 及其依赖项使用 XML 配置进行连接。

我试图解决的依赖问题:

Controller 正在获取 TestExecutionOrchestratorService 的对象。TestExecutionOrchestratorService 正在获取注入的依赖服务映射 [我可以通过打印出的日志消息来判断]。我将此 Map 作为实例变量保存在 TestExecutionOrchestratorService 中。但是,Controller 中的 TestExecutionOrchestratorService 对象的实例在依赖注入期间似乎没有设置 Map。换句话说:

Controller---DEPENDS ON--->TestExecutionOrchestratorService---DEPENDS ON--->Map[of JUnitTestExecutorService, QTPTestExecutorService] 在Controller中设置的TestExecutionOrchestratorService实例中Map为空。我从日志消息中知道在服务器启动期间正在注入 Map。

代码和 XML 文件如下:

清单 1 - 控制器

    /**
     * Handles requests for the application home page.
     */
    @Controller
    public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private TestExecutionOrchestratorService testExecutionOrchestratorService;

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        //ISSUE: Call to the service layer. The Map in the service is empty even though dependency injection is happening
                //there.
        if (testExecutionOrchestratorService != null) {
          logger.info("testExecutionOrchestratorService is not null. Executing it...");
          testExecutionOrchestratorService.runTests();
        }
        else {
          logger.info("testExecutionOrchestratorService is null. Why Why Why??");
        }


        return "home";
    }

}

清单 2 - TestExecutionOrchestratorService

    /*
     * This class is the entry point into test execution. It takes the request to execute tests
     * and calls the appropriate test executors.
     */

    @Service("testExecutionOrchestratorService")
    public class TestExecutionOrchestratorService implements TestResultsReporter {
    /* List of executor services */
    private Map<String, TestExecutorService> testExecutors = new HashMap<String, TestExecutorService>();

  private static final Logger logger = LoggerFactory.getLogger(TestExecutionOrchestratorService.class);



  /*
   * For Spring's dependency injection - to inject all the test executors.
   */
  public void setExecutormap(Map<String, TestExecutorService> exMap) {
    if (exMap != null) {
      Set<Entry<String, TestExecutorService>> mapEntrySet = exMap.entrySet();
      logger.error("TestExecutionOrchestratorService [setExecutorMap]: Got a map of test executors. The entries are:");
      for (Entry<String, TestExecutorService> mapE: mapEntrySet) {
        logger.error("Key: " + mapE.getKey() + ", Value: " + mapE.getValue().getExecutorName());
      }

      //ISSUE: testExecutors is showing as null in the "runTests" method that is called by the Controller. Why??
      testExecutors.putAll(exMap);  
    }
    else {
      logger.error("TestExecutionOrchestratorService [setExecutorMap]: Got a null executors map");  
    }
  }


  /* runTests - Calls upon the various executors to run the tests.
   * ISSUE: The Controller is calling this method but the Map set in 'setExecutorMap' is not being found. */
  public void runTests() {
    logger.error("TestExecutionOrchestratorService [runTests]: Entering the method");
    /* Create a unique test run ID. This will be the current time stamp. */
    String testRunTimestamp = new Timestamp(new Date().getTime()).toString(); 
    logger.error("TestExecutionOrchestratorService [runTests]: Will execute executors with test run ID: " + testRunTimestamp);
        /* Call each executor and ask them to run their default tests. */

        if ((testExecutors != null) && (!testExecutors.isEmpty())) {
          logger.error("TestExecutionOrchestratorService [runTests]: test executors are available. Will execute them.");
          Collection<TestExecutorService> teServices = testExecutors.values();
          for (TestExecutorService teService: teServices) {

          teService.runTests(testRunTimestamp, null, this);

          }
        }
        else {
          /* ISSUE: THIS IS WHERE EXECUTION IS ALWAYS COMING */
          logger.error("TestExecutionOrchestratorService [runTests]: There are no test executors available.");
        }

    }
}

清单 3 - Spring 的 Web 上下文 XML 文件 (root-context.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="..">


<!-- Root Context: defines shared resources visible to all other web components -->

<!-- Defining services -->
<!-- The following services are defined here: TestExecutionOrchestratorService,
JUnitTestExecutorService, QTPTestExecutorService -->
<!-- Definition of the JUnitTestExecutorService -->
<bean id="junitexecutor" name="junitexecutor" class="com.testing.autofwk.execution.JUnitTestExecutorService" />
<!-- Definition of the QTPTestExecutorService -->
<bean id="qtpexecutor" name="qtpexecutor" class="com.testing.autofwk.execution.QTPTestExecutorService" />
<!-- Definition of the TestExecutionOrchestratorService -->
<bean id="testexecutororchestrator" name="testexecutororchestrator" class="com.testing.autofwk.execution.TestExecutionOrchestratorService">
  <property name="executormap">
    <map>

       <entry key="junit">
         <ref local="junitexecutor"/>
       </entry>
       <entry key="qtp">
         <ref local="qtpexecutor"/>
       </entry>
    </map>
  </property>
</bean>


<context:component-scan base-package="com.testing.autofwk.execution" />

</beans>

清单 4 - MVC 应用程序调度程序的上下文 XML 文件 (servlet-context.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="..">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.testing.autofwk" />



</beans:beans>
4

1 回答 1

2

似乎您正在创建多个,TestExecutionOrchestratorService并且错误的一个被注入到您的控制器中。加载 root-context.xml 时会创建一个对象,并且由于TestExecutionOrchestratorService具有@Service注释,因此将在扫描类时创建其他 bean。

最重要的是,由于调度程序的上下文 XML 文件,某些包将被扫描两次。

在调度程序的上下文 XML 文件中使用类似这样的内容是一个很好的做法,以避免多次扫描同一个类:

<context:component-scan base-package="com.testing.autofwk" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

在根上下文中:

<context:component-scan base-package="com.testing.autofwk">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
于 2013-01-04T18:50:29.030 回答