java定时器无法自动注入的问题解析(原来Spring定时器可以这样注入service)

栏目: Java · 发布时间: 8年前

内容简介:java定时器无法自动注入的问题解析(原来Spring定时器可以这样注入service)

近些日子在做一个项目,在项目中需要用到spring的定时任务,但是在操作数据库的时候发现总是不能正确的进行数据的插入,经过查看才发现:是因为service层不能够通过普通的方法注入到action,所以这样就不能够执行service层,进而执行数据库的操作。

解决方法:这时需要配置一个service注入的 工具 类,代码如下:

一、工具类

public class ApplicationContextUtil implements ApplicationContextAware {  
        private static ApplicationContext applicationContext;  

        public static ApplicationContext getApplicationContext() {  
            return applicationContext;  
        }  

        public void setApplicationContext(ApplicationContext applicationContext) {  
            ApplicationContextUtil.applicationContext = applicationContext;  
        }  

        public static Object getBean(String beanName) {  
            return applicationContext.getBean(beanName);  
        }  
    }

二、配置bean

<bean  id ="applicationContextUtil"  class ="org.sihai.soil.util.ApplicationContextUtil" ></bean >

三、定时器中通过ApplicationContextUtil类,获得service。

realmEbi = (RealmEbi) ApplicationContextUtil.getBean("realmEbi");
        realmEbi.insertHour(RealmApplianceModel.sendData3);

完美解决了,这样我们就可以获取到service对象进行相对应的业务处理,而不需要再创建jdbc来操作了。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Chinese Authoritarianism in the Information Age

Chinese Authoritarianism in the Information Age

Routledge / 2018-2-13 / GBP 115.00

This book examines information and public opinion control by the authoritarian state in response to popular access to information and upgraded political communication channels among the citizens in co......一起来看看 《Chinese Authoritarianism in the Information Age》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换