一、主要是注意XML文件中設置3個地方和類文件中配置
第一步配置:
第二步配置:注解掃描
第三步配置:開啟任務
類中注解配置:如下
@Service 或者Component @Lazy(false) public class ImportGamexxjh5 { private static Logger logger = LoggerFactory.getLogger(ImportGamexxjh5.class); @Autowired Gamexxjh5Service gamexxjh5Service; @Autowired TfAnaysisResultTyhxService tfAnaysisResultTyhxService; @Scheduled(cron = "0 4 18 * * ?") public void importGameXXJH5() { logger.info("-------執行importGameXXJH5開始------->"+ DateUtils.getDateTime()); SimpleDateFormat myFmt = new SimpleDateFormat("yyMMdd"); Date date = new Date(); String nowdate = myFmt.format(date); String tablename="order_"+nowdate; rundata(tablename); logger.info("-------導入H5數據庫中"+tablename+"表的渠道匯總數據----"); logger.info("-------執行importGameXXJH5結束------->"+DateUtils.getDateTime()); } public void rundata(String tablename) { //數據源切至H5數據庫,獲取按渠道按天匯總數據 DynamicDataSource.setCurrentLookupKey("h5_dataSource"); List<Gamexxjh5> gList = gamexxjh5Service.queryAll(tablename); //數據源切換回至版權數據庫 DynamicDataSource.setCurrentLookupKey("dataSource"); //將數據導入至版權的數據表中 for (Gamexxjh5 item : gList) { TfAnaysisResultTyhx tfAnaysisResultTyhx =new TfAnaysisResultTyhx(); tfAnaysisResultTyhx.setChannelid(item.getChannelid()); tfAnaysisResultTyhx.setChannelname(item.getChannelname()); tfAnaysisResultTyhx.setDaypayment(item.getDaypayment()); tfAnaysisResultTyhx.setStatdate(item.getStatdate()); tfAnaysisResultTyhx.setGameid("41"); tfAnaysisResultTyhx.setGamename("新仙劍H5"); tfAnaysisResultTyhx.setGameEnglishName("xinxianjianH5"); tfAnaysisResultTyhx.setResult("Y"); tfAnaysisResultTyhx.setChanneltype(""); tfAnaysisResultTyhx.setIpowner("大宇資訊股份有限公司"); tfAnaysisResultTyhx.setIpownerid("8"); tfAnaysisResultTyhx.setMoneycl(item.getDaypayment()); tfAnaysisResultTyhx.setMoney(item.getDaypayment()); tfAnaysisResultTyhxService.save(tfAnaysisResultTyhx); } } }
二、總結:
配置文件中必要項:
- xmlns:task="http://www.springframework.org/schema/task"
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.1.xsd
- <task:annotation-driven />
類中注解必要項:
- @Service 或者Component
- @Lazy(false)
- @Scheduled(cron = "0 4 18 * * ?")
- 類中引用其他service層用屬性注入 @Autowired即可
三、注意事項
【1】main調用的時候不能通過 @Autowired 來進行注解,
因為你沒有啟動項目,相當於你注入的東西沒有沒有被使用,
使用main方法使用的話,必須使用getbean獲取實例對象。
【3】本版本采用的spring4.0 ,網上有文章寫
@Scheduled不支持屬性注入 @Autowired,
因為定時任務會在屬性注入之前執行,是不是spring的版本低的原因嗎,這個沒有親測過。
有問題歡迎留言
