Java NoSuchBeanDefinitionException:没有JobLauncher类型的符合autowire的bean

Java NoSuchBeanDefinitionException:没有JobLauncher类型的符合autowire的bean,java,spring,spring-mvc,spring-batch,autowired,Java,Spring,Spring Mvc,Spring Batch,Autowired,我跟在后面。现在,我想在命中一个API时运行这个作业,所以我把它做成了一个web项目&按照控制器实现,但是bean没有自动连接 示例.spring.batch.web.controller.JobLauncherController @Controller @RequestMapping("/run") public class JobLauncherController { @Autowired private JobLauncher jobLauncher; @Autowir

我跟在后面。现在,我想在命中一个API时运行这个作业,所以我把它做成了一个web项目&按照控制器实现,但是bean没有自动连接

示例.spring.batch.web.controller.JobLauncherController

@Controller
@RequestMapping("/run")
public class JobLauncherController {

  @Autowired
  private JobLauncher jobLauncher;

  @Autowired
  private Job job;

  @RequestMapping(method = RequestMethod.GET, value = "/runJob")
  @ResponseBody
  public void launchImportDataJob() throws Exception {
    jobLauncher.run(importDataJob, new JobParameters());
  }
}
/WEB-INF/spring/batch/applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <!-- enable auto-scan for all @Repository, @Service, @Controller java files 
    for beans -->
  <context:component-scan base-package="examples.spring">
  </context:component-scan>

  <!-- Import extra configuration -->
  <import resource="/WEB-INF/spring/batch/database.xml"/>
  <import resource="/WEB-INF/spring/batch/spring-batch.xml"/>
  <import resource="/WEB-INF/spring/batch/job.xml"/>

</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- stored job-metadata in database -->
  <bean id="jobRepository"
  class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="transactionManager" ref="transactionManager" />
  <property name="databaseType" value="mysql" />
  </bean>

    <bean id="transactionManager"
  class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />


    <bean id="jobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <property name="jobRepository" ref="jobRepository" />
    </bean>

</beans>
<web-app>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
web.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <!-- enable auto-scan for all @Repository, @Service, @Controller java files 
    for beans -->
  <context:component-scan base-package="examples.spring">
  </context:component-scan>

  <!-- Import extra configuration -->
  <import resource="/WEB-INF/spring/batch/database.xml"/>
  <import resource="/WEB-INF/spring/batch/spring-batch.xml"/>
  <import resource="/WEB-INF/spring/batch/job.xml"/>

</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- stored job-metadata in database -->
  <bean id="jobRepository"
  class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="transactionManager" ref="transactionManager" />
  <property name="databaseType" value="mysql" />
  </bean>

    <bean id="transactionManager"
  class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />


    <bean id="jobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <property name="jobRepository" ref="jobRepository" />
    </bean>

</beans>
<web-app>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

上下文配置位置
/WEB-INF/spring/applicationContext.xml
春天
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/spring-servlet.xml
1.
春天
/

在web.xml上使用Spring listener加载applicationContext.xml

<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

org.springframework.web.context.ContextLoaderListener

在我的应用程序类中使用以下内容可以在不干扰web.xml的情况下工作(我的项目中甚至没有web.xml):

另见

或者使用更简洁的版本

@SpringBootApplication
@ImportResource("applicationContext.xml")

您是否在web.xml文件的context param中声明了contextConfigLocation?谢谢@codiacTushki。有趣的是,我在阅读后声明了
侦听器
,但它仍然抛出异常(另一个异常=D),因此删除了声明。