(澳门正规博彩十大网站) springmvc定时任务注入问题

数据库中有一个字段会不断更新,并且本地有一个文件存储着这个字段的信息,我写了一个定时器,自动更新文件,但是出问题了,下面是定时器代码:

package org.ramer.diary.util; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.ramer.diary.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; /** * schedule job: * execute every ten seconds; * query tags from table topic and remove duplicates ,and compare to local file "/xml/tags.xml"; * new tags will be append to file. * * @author ramer * */
@Controller
public class ScheduleWork implements ServletContextListener { /** * get millis for time * @param time "HH:mm:ss" * @return */ private long getTimeMillisString time { try { DateFormat dateFormat = new SimpleDateFormat"yy-MM-dd HH:mm:ss"; DateFormat dayFormat = new SimpleDateFormat"yy-MM-dd"; Date curDate = dateFormat.parsedayFormat.formatnew Date + " " + time; return curDate.getTime; } catch Exception e { e.printStackTrace; } return 0; } @Override public void contextDestroyedServletContextEvent arg0 { } @Autowired TopicService topicService; @Value"#{diaryProperties[tags.xml.position]}" private String file; @Override public void contextInitializedServletContextEvent servletContextEvent { ScheduledExecutorService executor = Executors.newScheduledThreadPool1; // execute every ten seconds long oneDay = 10 * 1000; SimpleDateFormat simpleDateFormat = new SimpleDateFormat"HH:mm:ss"; // start now long initDelay = getTimeMillissimpleDateFormat.formatnew Date - System.currentTimeMillis; initDelay = initDelay > 0 ? initDelay : oneDay + initDelay; // start execute job executor.scheduleAtFixedRate -> { System.out.println"----------start update tags-----------"; // tags in database System.out.printlntopicService; System.out.printlnfile; List<String> tags = topicService.getAllTags; // remove duplicate StringBuilder stringBuilder = new StringBuilder; for String string : tags { stringBuilder.appendstring + ";"; } String[] strings = stringBuilder.toString.split";"; // Arrays.asList will return a proxy with doesnt implement add and remove,so create a list. List<String> tagslist = Arrays.asListstrings; tagslist = new ArrayList<>tagslist; for int i = 0; i < tagslist.size; i++ { for int j = i + 1; j < tagslist.size; j++ { if tagslist.geti.equalstagslist.getj { tagslist.removej; j--; } } } // tags no duplicate. tags = tagslist; System.out.println"tags in database: "; for String string : tags { System.out.println"	" + string; } List<String> tagsInFile = new ArrayList<>; try { // read tags in local file tagsInFile = FileUtils.readTagfile, servletContextEvent.getServletContext; } catch Exception e { System.out.println"Exception ScheduleWorkLine 111"; e.printStackTrace; } System.out.println"文件中的tags: "; for String string : tagsInFile { System.out.println"	" + string; } // less than tags in database ,so each the tags in local file for int i = 0; i < tags.size; i++ { if !tagsInFile.containstags.geti { tagsInFile.addtags.geti; i++; } } System.out.println"update tags: "; for String string : tagsInFile { System.out.println"	" + string; } try { // update tags in local file FileUtils.writeTagtagsInFile, file, servletContextEvent.getServletContext; } catch Exception e { System.out.println"Exception ScheduleWorkLine 129"; e.printStackTrace; } }, initDelay, oneDay, TimeUnit.MILLISECONDS; } }

运行时,发现上面的‘topicService’和file总是null,也就是注入失败了,请问是什么原因呢?

我发现你这个类用的注解是 @Controller,是不是 spring-mvc 包扫描的配置没有扫描你这个 ScheduleWork 类所在的org.ramer.diary.util 包?

配置发出来!

发表评论

电子邮件地址不会被公开。 必填项已用*标注