2013-07-16 61 views
1

我正在嘗試在我的Spring項目中使用@Async註釋。爲此,我將此行添加到我的servlet-config.xml中: <task:annotation-driven />。因此,我不能再運行該項目,我得到這個錯誤:無法設置@Async方法

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.dynamease.web.user.social.LinkedInController]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given 

這裏是我LinkedInController類:

@Controller 
@Scope(proxyMode = ScopedProxyMode.INTERFACES) 
public class LinkedInController { 

    private static final Logger logger = LoggerFactory.getLogger(LinkedInController.class); 

    private final LinkedIn linkedIn; 

    @Inject 
    public LinkedInController(LinkedIn linkedIn) { 
     logger.info("Initialisation du controleur linkedIn."); 
     this.linkedIn = linkedIn; 
    } 

    @RequestMapping(value = "linkedin") 
    public ModelAndView categorize() { 
     categorizeAndStore(linkedIn); 
     return mav; 
    } 

    @Async 
    public Future<Boolean> categorizeAndStore(LinkedIn source) { 
     // java stuff 
     return new AsyncResult<Boolean>(true); 
    } 
} 

我找到解決的辦法是增加<aop:scoped-proxy>@Scope(proxyMode = ScopedProxyMode.INTERFACES)但作爲你可以看到,它的存在並不重要。

回答

1

如果方面應用於使用CGLIB代理的類,則需要不帶參數的構造函數。嘗試是這樣的:

@Controller 
public class LinkedInController { 

    private static final Logger logger = LoggerFactory.getLogger(LinkedInController.class); 

    @Inject 
    private final LinkedIn linkedIn; 

    public LinkedInController() { 
     logger.info("Initialisation du controleur linkedIn."); 
    } 

    @RequestMapping(value = "linkedin") 
    public ModelAndView categorize() { 
     categorizeAndStore(linkedIn); 
     return mav; 
    } 

    @Async 
    public Future<Boolean> categorizeAndStore(LinkedIn source) { 
     // java stuff 
     return new AsyncResult<Boolean>(true); 
    } 
} 
+0

那麼,它不運行知道,我很感激那個:) 但是,不asynchronymously執行categorizeAndStore()方法,任何想法,爲什麼? – fxm

+2

該方法必須從不同的對象中調用,如果您從同一個對象內調用該方法,它不會被攔截......請記住,它使用AOP ... http://static.springsource.org/ spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies –

+0

好吧,我會試試看。再次感謝 ! – fxm