How to go about Spring autowiring?

跟風遠走 提交于 2019-12-11 12:05:27

问题


public class ProcessSchedulerServlet implements javax.servlet.Servlet {
    Timer timer=new Timer();

    @Override
    public void init(ServletConfig arg0) throws ServletException {
        timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            LogProcessorService logProcessorService=new LogProcessorServiceImpl();
            logProcessorService.processPageRequestsLogs();
        }
     }, 60*1000, 120*1000);
}

This is ugly and it doesn't work, anyway. The LogProcessorServiceImpl has properties with @Autowired annotation. These properties are not autowired when this code runs. This may be expected.

The real question is: how to make this run() method work. It seems to me that Spring wants the logProcessorService to be autowired to have properties within LogProcessorServiceImpl autowired, as well.

=== SCENARIO 1 ==============================================================

public void run() {
    final LogProcessorService logProcessorService=null;
    WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getAutowireCapableBeanFactory().autowireBean(logProcessorService);
    logProcessorService.processPageRequestsLogs();
}

Result: compile time error: Cannot refer to a non-final variable arg0 inside an inner class defined in a different method

=== SCENARIO 2 ==============================================================

@Autowired
LogProcessorService logProcessorService;
public void run() {
    logProcessorService.processPageRequestsLogs();
}

Result: run time error: logProcessorService is null;

==== SOLUTION (from Boris) ======================================================

public class ProcessSchedulerServlet implements javax.servlet.Servlet {
    Timer timer=new Timer();

    @Autowired
    LogProcessorService logProcessorService;

    @Override
    public void init(ServletConfig arg0) throws ServletException {
        final AutowireCapableBeanFactory autowireCapableBeanFactory=WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getAutowireCapableBeanFactory();
        autowireCapableBeanFactory.autowireBean(this);

        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                logProcessorService.processPageRequestsLogs();
            }
        }, 60*1000, 120*1000);
}

回答1:


Why bother with servlets and Timer class if Spring has a built in scheduling support:

@Service
public class LogProcessorService {

    @Scheduled(fixedRate=120*1000, initialDelay=60*1000)
    public void processPageRequestsLogs() {
        //...
    }

}

That's it! No timers, runnables and servlets. Note: initialDelay was introduced in Spring 3.2 M1 (see SPR-7022).



来源:https://stackoverflow.com/questions/13437913/how-to-go-about-spring-autowiring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!