Spring定时任务task(Quartz) bean 无法注入
来源:互联网 发布:淘宝代购怎么辨别真假 编辑:程序博客网 时间:2024/06/11 12:39
最近项目中使用到定时任务,通过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;@Servicepublic 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 */@Componentpublic 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 */@Componentpublic class QuartzTask { @Autowired private QuartzService quartzService; public void run(){ quartzService.appService(); }}
阅读全文
0 0
- Spring定时任务task(Quartz) bean 无法注入
- Spring+Quartz 定时任务无法自动注入bean的问题
- spring+quartz实现定时任务遇到问题总结(bean无法注入)
- spring Quartz和 Spring Task定时任务
- EJB3.0变通实现定时任务quartz,schedule ,task,EJB3.0注入Quartz无法注入,注入失败,NullpiontException
- spring中quartz和task定时任务
- JAVA定时任务Timer、Spring Task、Quartz
- quartz springh和 spring-task 定时任务
- 【Spring】Quartz和Task实现定时任务
- 定时任务之Timer,Quartz,Spring task
- 定时任务Quartz和spring task
- Spring quartz定时任务Service注入问题
- Spring-quartz定时任务service注入问题
- Spring+Quartz无法自动注入bean问题
- spring实现自动定时任务调度【Spring Task】【Quartz】
- Spring整合Quartz时,任务类中无法注入Bean的问题
- Spring定时任务的几种实现 spring框架 quartz spring spring-task 定时任务 注解
- Spring task quartz 定时任务的几种实现
- Google在东京搬出了所有的AI产品和技术,你知道哪个? | 精选
- 使用cargo-maven2-plugin插件自动化发布到Tomcat服务器
- 厉害了!新西兰出现了世界上第一个机器人公务员 | 前沿
- vmware虚拟机新建安装windows2000操作系统
- 虚拟私助理要来了,五个行业将受影响人 | 分析
- Spring定时任务task(Quartz) bean 无法注入
- HTML视频
- vue学习笔记3
- 修改指针指向的QVector内部成员的值
- Apple Watch开发和数据传输(一)
- 非比较排序—计数排序&基数排序
- 常见烧写、读取工具
- 百度与小米在IoT上深度合作 陆奇和雷军都说了什么? | 资讯
- 一步步实现WebServer中间件——自己实现一个线程池