2016-02-03 91 views
2

我創建了一個Spring Boot應用程序。我配置了包含調度器方法startService()的類。 下面是我的代碼:Scheduler沒有在Spring Boot中運行

服務類:

package com.mk.service; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.scheduling.annotation.Scheduled; 
import org.springframework.stereotype.Component; 

import com.mk.envers.model.BossExtChange; 
import com.mk.envers.model.BossExtChangeRepository; 

@Component 
public class EnverseDemoService { 

    @Autowired 
    BossExtChangeRepository bossExtChangeRepository; 

    @Scheduled(fixedRate = 30000) 
    public void startService() { 
     System.out.println("Calling startService()"); 
     BossExtChange bossExtChange = bossExtChangeRepository.findById(5256868L); 
     System.out.println("bossExtChange.getDescription()--->"+bossExtChange.getDescription()); 
     System.out.println("Ending startService()"); 
    } 
} 

主類:

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

@SpringBootApplication 
@EnableScheduling 
@PropertySource("classpath:application.properties") 
public class EnverseDemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(EnverseDemoApplication.class, args); 
    } 
} 

我已經註解,將作爲運行類作爲@Component也是方法@Scheduled(fixedRate = 30000)一個調度程序。但是,當以Spring Boot運行應用程序時,調度程序不會觸發。控制檯顯示以下消息:

2016-02-03 10:56:47.708 INFO 10136 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
2016-02-03 10:56:47.721 INFO 10136 --- [   main] com.mk.envers.EnverseDemoApplication  : Started EnverseDemoApplication in 3.231 seconds (JVM running for 3.623) 
2016-02-03 10:56:47.721 INFO 10136 --- [  Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]49e202ad: startup date [Wed Feb 03 10:56:44 IST 2016]; root of context hierarchy 
2016-02-03 10:56:47.721 INFO 10136 --- [  Thread-2] o.s.j.e.a.AnnotationMBeanExporter  : Unregistering JMX-exposed beans on shutdown 
2016-02-03 10:56:47.736 INFO 10136 --- [  Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 

任何人都可以請幫助我。

+0

刪除'@ PropertySource'作爲一個已經被默認加載解決這個問題。 'EnverseDemoApplication'是否在'EnverseDemoService'的相同或超級包中? (你省略了'package'子句,所以很難說。)。如果它不是你的bean沒有拿起,並且沒有調度/線程將活着。 –

回答

6

我終於能夠解決上述問題了,我將我的服務類EnverseDemoService的包從package com.mk.service;更改爲com.mk.envers.service;。這是因爲如果包com.mk.envers中存在主配置類EnverseDemoApplication。引導應用程序中的所有其他類都應該位於合格包中。 Eg: com.mk.envers.*;

8

可能是你可以在配置文件中添加@ComponentScan註釋

@SpringBootApplication 
@EnableScheduling 
@ComponentScan(basePackages = "com.mk.service") 
@PropertySource("classpath:application.properties") 
public class EnverseDemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(EnverseDemoApplication.class, args); 
    } 
} 
相關問題