2

目前在春季3.2.3上工作。我們已經開發了一個API應用程序,現在已經有2年了。它運作良好。但是隨着bean類的增加,有關循環依賴的例外。所以我們通過在application-context.xml中更改default-lazy-init =「true」來解決這個問題。但是,這導致了另一個關於@Scheduled的挑戰根本無法實現。春季註釋懶惰負載

注意:我們的應用程序使用spring註釋,因此bean沒有在xml文件中聲明。 下面是我的參考代碼:

的applicationContext.xml

<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"> 

服務類

@Service("notificationService") 
public class NotificationService{ 

    @Scheduled(cron = "0 09 11 * * *") 
    @Async 
    public void sampleNotificaton(){ 

    } 
} 

嘗試在提供服務的水平,但循環依賴例外添加@Lazy(假)仍時有發生。

感謝您的建議。 謝謝。

+0

我有同樣的問題,但是,未找到任何解決方案? – Nilesh

+0

我會嘗試首先解決循環依賴。這是很大的設計氣味,一些DI容器根本不允許它。 – luboskrnac

+0

你試過'@ EnableScheduling'註解嗎? – luboskrnac

回答

1

因爲我們有限制不能打破循環依賴(其中大部分是很容易BTW解決),我會嘗試略施小,調度的註釋將被提取到單獨的bean:

@Component 
public class NotificationScheduler { 
    @Autowired //I prefer contructor injection, but field injection might be needed in this case because or circular dependency 
    private NotificationService notificationService; 

    @Scheduled(cron = "0 09 11 * * *") 
    public void sampleScheduling() { 
     notificationService.sampleNotification(); 
    } 
} 

@Service("notificationService") 
public class NotificationService{ 

    @Async 
    public void sampleNotificaton(){ 

    } 
}