Spring MVC:在创建异常中异步提供bean

时间:2015-04-02 10:01:55

标签: java spring spring-mvc asynchronous

我正在开发一个Spring-MVC应用程序,在这个应用程序中,我想至少使用@Async作为即发即弃的方法。当我尝试使用@Async并且我也为类使用@EnableAsync注释时,不执行方法内的操作。当我在servlet-context.xml中添加任务执行器时,我得到一个当前正在创建的错误bean。我是Async的新手,任何人都可以告诉我如何使用它。 我没有使用Eager加载顺便说一句。 错误日志:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'groupNotesService': Bean with name 'groupNotesService' has been injected into other beans [mattachService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

代码: GroupNotesServiceImpl:

@Service
@Transactional
@EnableAsync
public class GroupNotesServiceImpl implements GroupNotesService {

 @Override
    @Async
    public void editGroupNote(GroupNotes mnotes, int msectionId) {
//Code to be executed, which is not getting executed
    }

}

Servlet-context.xml:

 <task:annotation-driven executor="executor" />
    <task:executor id="executor" pool-size="20"/>

    <mvc:annotation-driven />

    <mvc:default-servlet-handler />

    <resources mapping="/resources/" location="/resources/" />

如果我删除上面的任何mvc行,我得到一个servlet.init()抛出加载异常错误。

此外,是否可以在返回int的地方使用Async?我查看了Future标签,但我不知道需要进行哪些修改。 这是返回int的方法。

 @Override
    public int saveGroupNoteAndReturnId(GroupNotes mnotes, int msectionid) {
  // saves note and returns its id.
}

MattachService bean:

<beans:bean id="mattachDAO"
        class="com.journaldev.spring.dao.GroupAttachmentsDAOImpl">
        <beans:property name="sessionFactory"
            ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

    <beans:bean id="mattachService"
        class="com.journaldev.spring.service.GroupAttachmentsServiceImpl">
        <beans:property name="groupAttachmentsDAO" ref="mattachDAO" />
    </beans:bean>

修改的 我检查了在一个类中运行@Transactional和@Async都存在问题。 Jira SPR-7147。解决方法建议引入正常的外观,我真的不知道这意味着什么。

1 个答案:

答案 0 :(得分:0)

@EnableAsync应该在配置中,而不是服务本身。由于您似乎使用xml配置,请检查此https://stackoverflow.com/a/20127051/562721。它建议声明org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor以处理@Async注释