最近项目中使用到定时任务,通过Spring管理,但是在写定时任务出现 bean 无法注入问题,通过在网上查找大量资料也没要找到问题,试过网上多种方法也无效,通过与朋友多方讨论最终解决问题,现在将其分享给大家
问题是没有扫描到,只需在相应xml加上扫描即可
<context:component-scan base-package="com.***.log"/>
这是第一次写博客,希望大家多多指点
下面是我写的测试代码
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc_001</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置spring框架的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 使用上下文参数指定spring配置文件的位置 -->
<!-- applicationContext对象仅加载一次,服务器启动时加载,使用web中的监听器机制 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置 springmvc 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载 springmvc 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<!-- 注解的映射器 配置注解适配器 使用下边mvc:annotation-driven代替 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 使用组件扫描 -->
<context:component-scan base-package="com.***.log"/>
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
</bean> -->
</beans>
Service
用于bean 注入给定时任务
package com.***.***.service.impl;
import org.springframework.stereotype.Service;
import com.***.***.service.QuartzService;
@Service
public class QuartzServiceImpl implements QuartzService {
@Override
public void appService() {
System.out.println(QuartzServiceImpl.class);
}
}
方法一:使用spring自带定时任务
applicationContext-task.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 在这里添加包扫描即可解决 bean 无法注入问题 -->
<context:component-scan base-package="com.***.log"/>
<!-- 定时器开关 -->
<task:annotation-driven/>
<bean id="logTask" class="com.***.***.task.LogTask"/>
<task:scheduled-tasks>
<!-- 这里表示的是每隔20秒执行一次 -->
<task:scheduled ref="logTask" method="run" cron="0/20 * * * * ?"/>
</task:scheduled-tasks>
</beans>
定时任务
import org.springframework.beans.factory.annotation.Autowired;
import com.***.***.service.QuartzService;
/**
* 定时任务类
*
* @author XXX
*/
@Component
public class LogTask {
@Autowired
private QuartzService quartzService;
public void run(){
quartzService.appService();
}
}
方法二:Spring整合Quartz
这里其实和上面的基本一样 都是添加包扫描即可
applicationContext-quart.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<context:component-scan base-package="com.***.log"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.***.***.task.QuartzTask"/>
</property>
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 定时 触发器 -->
<bean id="beanNameAware" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="job"/>
</property>
<property name="cronExpression">
<value>0/20 * * * * ?</value>
</property>
</bean>
<!-- Quartz 调度器的 SchedulerFactoryBean -->
<bean id="smartLifecycle" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="beanNameAware"/>
</list>
</property>
</bean>
</beans>
定时任务
package com.***.***.task;
import org.springframework.beans.factory.annotation.Autowired;
import com.***.***.service.QuartzService;
/**
* 定时任务类
*
* @author XXX
*/
@Component
public class QuartzTask {
@Autowired
private QuartzService quartzService;
public void run(){
quartzService.appService();
}
}