论坛首页 Java企业应用论坛

Spring定时器问题

浏览 4868 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-12-09  
我想用Spring【采用的是SPRING+MyBatis】做一个定时器,进行任务调度。定时器在tomcate服务起来后,能够正常工作,几秒钟能够自动抛出事件。但当我从数据库中取配置参数的时候,定时器就不能正常工作了。

以下是正常工作的代码:
package cgs.rps.schdl.control;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.text.SimpleDateFormat;
import cgs.rps.system.domain.ParamConf;
import cgs.rps.system.service.ParamConfService;

@Service
public class ScheduleJob{

private ParamConfService paramConfService;

public void work(){
/*
ParamConf pc=paramConfService.getParamConf("FIN_AUTO_METHOD");
String autoMethod=pc.getParamCode().toUpperCase();
*/
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateFormat=new Date();

System.out.println("自动化调度任务:FIN_AUTO_METHOD=Y !"+" 【"+bartDateFormat.format(dateFormat)+"】");
/*
if("Y".equals(autoMethod)){
System.out.println("自动化调度任务:FIN_AUTO_METHOD=Y !"+" 【"+bartDateFormat.format(dateFormat)+"】");
}else{
System.out.println("手工调度调度任务:FIN_AUTO_METHOD=N !"+" 【"+bartDateFormat.format(dateFormat)+"】");
}*/

}
}


自动化调度任务:FIN_AUTO_METHOD=Y ! 【2012-12-09 15:36:55】
自动化调度任务:FIN_AUTO_METHOD=Y ! 【2012-12-09 15:37:00】
自动化调度任务:FIN_AUTO_METHOD=Y ! 【2012-12-09 15:37:05】
自动化调度任务:FIN_AUTO_METHOD=Y ! 【2012-12-09 15:37:10】
自动化调度任务:FIN_AUTO_METHOD=Y ! 【2012-12-09 15:37:15】

我把这两行的注释去掉:
ParamConf pc=paramConfService.getParamConf("FIN_AUTO_METHOD");
String autoMethod=pc.getParamCode().toUpperCase();
定时器就不能工作了。
我如果通过直接JDBC连接取数据库中的参数配置,定时器也没有问题。但只要我用这些Service类:ParamConfService ,就不能工作。为什么?

ParamConfService代码示例:
package cgs.rps.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cgs.rps.system.domain.ParamConf;
import cgs.rps.system.persistence.ParamConfMapper;

@Service
public class ParamConfService {
@Autowired
private ParamConfMapper paramConfMapper;

/**
* 获取主键id
* @return
*/
public Integer getParamId() {
return paramConfMapper.getParamId();
}

/**
* 获取某个参数配置信息
* @param param
* @return
*/
public ParamConf getParamConf(Map<String,Object> param) {
return paramConfMapper.getParamConf(param);
}

/**
* 通过匹配代码获取参数
*/
public ParamConf getParamConf(String matchName) {
Map<String,Object> param = new HashMap<String,Object>();

param.put("matchName", matchName);
return paramConfMapper.getParamConf(param);
}
}
   发表时间:2012-12-11  

@Service
public class ScheduleJob{
@Autowired
private ParamConfService paramConfService;
0 请登录后投票
   发表时间:2012-12-12  
你好,这个注解我当时是加过,仍然是不行。@Autowired
现在已经改变策略了。
0 请登录后投票
   发表时间:2012-12-12  
@Service
public class ScheduleJob{
@Autowired
private ParamConfService paramConfService;
0 请登录后投票
   发表时间:2012-12-12   最后修改:2012-12-13
ScheduleJob只是个普通类,普通类中增加@Service注解是不起作用的。如想获得业务类组建,可以从applicationContext上下文中获取:
在spring的配置文件中增加bean
<bean id="springContextHolder" class="com.huilan.eps.framework.utils.spring.SpringContextHolder" lazy-init="false"/>
其类如下:
package com.huilan.eps.framework.utils.spring;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
* @author alex
*/
@Component
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

/**
* Spring的ApplicationContext
*/
private static ApplicationContext applicationContext = null;

/**
* 日志
*/
private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*
* @param applicationContext ApplicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) {
logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext);

if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
+ SpringContextHolder.applicationContext);
}

//NOSONAR
SpringContextHolder.applicationContext = applicationContext;
}

/**
* 实现DisposableBean接口,在Context关闭时清理静态变量.
*
* @throws Exception Exception
*/
public void destroy() throws Exception {
SpringContextHolder.clear();
}

/**
* 取得存储在静态变量中的ApplicationContext.
*
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}

/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*
* @param name bean名称
* @param <T> 泛型类型
* @return Spring中的bean对象
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}

/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*
* @param requiredType 转换的类型
* @param <T> 泛型类型
* @return Spring中的bean对象
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
}

/**
* 从静态变量applicationContext中取得Beans, 自动转型为所赋值对象的类型.
*
* @param requiredType 请求类型
* @param <T> 泛型
* @return 该类型及其子类的集合
*/
public static <T> Map<String,T> getBeans(Class<T> requiredType){
assertContextInjected();
return applicationContext.getBeansOfType(requiredType);
}

/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clear() {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
applicationContext = null;
}

/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}

将你的类改一下
public class ScheduleJob{

private ParamConfService paramConfService= (ParamConfService )SpringContextHolder .getBean("paramConfService");
..........
}
0 请登录后投票
   发表时间:2013-04-18  
zxingchao2005 写道
ScheduleJob只是个普通类,普通类中增加@Service注解是不起作用的。如想获得业务类组建,可以从applicationContext上下文中获取:
在spring的配置文件中增加bean
<bean id="springContextHolder" class="com.huilan.eps.framework.utils.spring.SpringContextHolder" lazy-init="false"/>
其类如下:
package com.huilan.eps.framework.utils.spring;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
* @author alex
*/
@Component
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

/**
* Spring的ApplicationContext
*/
private static ApplicationContext applicationContext = null;

/**
* 日志
*/
private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*
* @param applicationContext ApplicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) {
logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext);

if (SpringContextHolder.applicationContext != null) {
logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
+ SpringContextHolder.applicationContext);
}

//NOSONAR
SpringContextHolder.applicationContext = applicationContext;
}

/**
* 实现DisposableBean接口,在Context关闭时清理静态变量.
*
* @throws Exception Exception
*/
public void destroy() throws Exception {
SpringContextHolder.clear();
}

/**
* 取得存储在静态变量中的ApplicationContext.
*
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}

/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*
* @param name bean名称
* @param <T> 泛型类型
* @return Spring中的bean对象
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
assertContextInjected();
return (T) applicationContext.getBean(name);
}

/**
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*
* @param requiredType 转换的类型
* @param <T> 泛型类型
* @return Spring中的bean对象
*/
public static <T> T getBean(Class<T> requiredType) {
assertContextInjected();
return applicationContext.getBean(requiredType);
}

/**
* 从静态变量applicationContext中取得Beans, 自动转型为所赋值对象的类型.
*
* @param requiredType 请求类型
* @param <T> 泛型
* @return 该类型及其子类的集合
*/
public static <T> Map<String,T> getBeans(Class<T> requiredType){
assertContextInjected();
return applicationContext.getBeansOfType(requiredType);
}

/**
* 清除SpringContextHolder中的ApplicationContext为Null.
*/
public static void clear() {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
applicationContext = null;
}

/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}

将你的类改一下
public class ScheduleJob{

private ParamConfService paramConfService= (ParamConfService )SpringContextHolder .getBean("paramConfService");
..........
}

您好,谢谢,我试试。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics