1

How to change my code to make it work?

public class GetDataFromTheWarehouse implements ServletContextListener {

    @Autowired
    ScheduledTask scheduledTask;

    private ScheduledExecutorService scheduler = null;

    public GetDataFromTheWarehouse() {
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            System.out.println("Scheduler Shutting down successfully " + new Date());
            scheduler.shutdown();
        } catch (Exception ex) {
        }
    }

    public void contextInitialized(ServletContextEvent arg0) {
        if ((scheduler == null) || (!scheduler.isTerminated())) {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.scheduleAtFixedRate(scheduledTask, 0, 60*60, TimeUnit.SECONDS);
        }
    }
}

Following is the ScheduledTask class, in which productService is null, so will fail every time calling productService.save():

@Component
public class ScheduledTask extends TimerTask {
    @Autowired
    ProductService productService;

    public void run() {
        try {
            parse();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void parse() throws IOException {
            ...
            productService.save(product);
            ...
        }
    }
}

My applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Enable autowire -->
    <context:component-scan base-package="com" />
    <context:annotation-config />

    <bean id="usersUpdateTask" class="com.demo.task.ScheduledTask">
    </bean>

    <mvc:annotation-driven /> 

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:resources mapping="/views/**" location="/views/" />
    <mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/fonts/**" location="/fonts/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/demo" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- Session Factory Declaration -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.demo.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
                <prop key="hibernate.default_schema">demo</prop>
                <prop key="format_sql">true</prop>
                <prop key="use_sql_comments">true</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

     <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

My demo structure:

demo website structure

abaghel
  • 14,783
  • 2
  • 50
  • 66
J.W
  • 671
  • 7
  • 23
  • An `@Autowired` field cannot be `null`. If that would happen the startup of your application will fail with a `BeanCreationException`. Now if it is `null` it is because you are creating instances yourself and not spring. You are autowiring a `ServletContextListener` which generally isn't spring managed and as such won't receive anything autowired. – M. Deinum Aug 24 '16 at 08:27
  • Thanks. So after Tomcat starting the website, if I don't use `ServletContextListener` and want to do some scheduled tasks in which `@Autowired` `productService` will be applied, what solutions I can have a try? – J.W Aug 25 '16 at 22:49

1 Answers1

0

Why do you have ScheduledTask initialized in appilcationConfig.xml when you are already using @Component for that bean class. Remove this from your applicationContext.xml file.

<bean id="usersUpdateTask" class="com.demo.task.ScheduledTask">
</bean>

EDIT : Adding Spring Boot Sample.

Here is the sample code for @Scheduled with @Autowired in Spring Boot.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-scheduler</groupId>
<artifactId>springboot-scheduler-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.4.0.RELEASE</version>
    </dependency>
 </dependencies>
</project>

MySpringBootApp.java

package my.spring.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class MySpringBootApp {
    public static void main(String[] args) {
    SpringApplication.run(new Object[] { MySpringBootApp.class }, args);
   }
}

MyService.java

package my.spring.app;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;

@Component
public class MyService {    
public String getNextMessage(){
     return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date());
   }
}

ScheduledTask.java

package my.spring.app;

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

@Component
public class ScheduledTask {

@Autowired
MyService service;

@Scheduled(fixedRate = 5000)
public void process() {
    System.out.println("Processing at " + service.getNextMessage());
    }
}

Sample output

Processing at 2016-08-24T14:01:48
Processing at 2016-08-24T14:01:53
Processing at 2016-08-24T14:01:58

Hope this helps.

EDIT-2 : Adding Spring Boot Sample war file version. Tested with Tomcat 8. Following two files are changed. Others are same as above.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-scheduler</groupId>
<artifactId>springboot-scheduler-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
     <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

</project>

MySpringBootApp.java

package my.spring.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class MySpringBootApp extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MySpringBootApp.class);
  }

public static void main(String[] args) {

    SpringApplication.run(new Object[] { MySpringBootApp.class }, args);

    }
}
abaghel
  • 14,783
  • 2
  • 50
  • 66
  • Thanks for your suggestion :) My original question explained my situation, but was edited(deleted) by David Makogon. Here is the deleted content: I found there are similar questions, I followed those solutions but didn't work(maybe I misunderstood them): Spring @autowired annotation with java TimerTask doesn't work Spring autowired annotation with java timertask How How to change my code to make it work? Please show worked codes if possible. I am a Java newbie, I followed some tutorials to make this demo and there may be lots of codes or structures look weird. – J.W Aug 22 '16 at 22:39
  • Any specific reason why do you want to use TimerTask? You can use Spring " @Scheduled" annotation in parse() method of your ScheduledTask class. Please see the spring guide at https://spring.io/guides/gs/scheduling-tasks/ – abaghel Aug 23 '16 at 13:05
  • Thanks. But the original problem still exists: Can not use Autowired ProductService productService in a scheduled task (productService is null). Do you have any sample code that Autowired works in scheduled tasks? – J.W Aug 24 '16 at 06:15
  • Can you please paste your updated applicationContext.xml and ScheduledTask file? – abaghel Aug 24 '16 at 07:13
  • I think the reason why '@Autowired' didn't work is as @M. Deinum mentioned: "You are autowiring a ServletContextListener which generally isn't spring managed and as such won't receive anything autowired." – J.W Aug 26 '16 at 01:23
  • Hi @abaghel I followed your sample code, but still didn't work. I use Eclipse Java EE IDE for Web Developers, Version: Kepler Service Release 2, Build id: 20140224-0627, "File >> New >> Maven Project", create the class name and code exactly as yours. Then I add this project into Tomcat v7.0 and start Tomcat, but In the _Console_ tab there no output message generated by `getNextMessage()`. What IDE you are using? Is there any step I did wrong? – J.W Aug 26 '16 at 01:46
  • Sample code I put is Spring Boot application. You can run maven package in eclipse. When build is ready you can run MySpringBootApp class as Run As > Java Program. You will see message in eclipse console. – abaghel Aug 26 '16 at 03:23
  • Cool, the sample code works :) But how can I let scheduled tasks work (should involve @Autowired function) after starting Tomcat rather than "run MySpringBootApp class as Run As > Java Program"? – J.W Aug 26 '16 at 07:26
  • Updated pom.xml and MySpringBootApp.java for war file support in spring boot. (Check EDIT-2) No change in ScheduledTask.java and MyService.java. Run maven package to create war file. Deploy in Tomcat 8. You will see the message in tomcat console. Hope this helps. – abaghel Aug 26 '16 at 09:05
  • Thank you, it works finally. I guess my Eclipse Kepler has some problems and could not make it work at first. I installed Eclipse Neon and simply worked it out. – J.W Aug 27 '16 at 06:55