Spring:添加@Async注释后,Bean当前处于创建错误状态

时间:2016-06-21 09:24:14

标签: java spring spring-mvc asynchronous

我正在开发一个Spring-MVC应用程序,其中我有一个服务层类,在那里我想要使用@Async注释来转换文档。不幸的是,当我添加异步注释时,项目没有启动,我得到一个当前处于创建异常的bean。

错误日志:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'groupAttachmentsServiceImpl': Bean with name 'groupAttachmentsServiceImpl' has been injected into other beans [groupNotesDAOImpl,personalAttachmentServiceImpl,personalNoteServiceImpl,driveQuickstartImpl,dropBoxTaskImpl,groupNotesServiceImpl,groupSectionServiceImpl,groupCanvasServiceImpl] 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.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:568) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 99 common frames omitted

代码:

@Service
@Transactional
public class GroupAttachmentsServiceImpl implements GroupAttachmentsService {

    private final GroupAttachmentsDAO groupAttachmentsDAO;
 @Autowired
    public GroupAttachmentsServiceImpl(GroupAttachmentsDAO groupAttachmentsDAO) {
        this.groupAttachmentsDAO = groupAttachmentsDAO;
    }

@Async
    @Override
    public CompletableFuture<String> createPPtxPreview(String path) {
CompletableFuture<String> preview = new CompletableFuture<>();
            preview.complete("data:image/png;base64," + base64Encoder.encode(baos.toByteArray()));
                       return preview;


        }
}

XML配置:

<context:component-scan base-package="com.myproj.spring">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <context:property-placeholder location="classpath:application.properties"/>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver"/>
        <beans:property name="url"
                        value="URL"/>
        <beans:property name="username" value="USER"/>
        <beans:property name="password" value="PASSWORD"/>
        <beans:property name="removeAbandoned" value="true"/>
        <beans:property name="removeAbandonedTimeout" value="20"/>
        <beans:property name="defaultAutoCommit" value="false"/>
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="packagesToScan" value="com.tooltank.spring.model"/>

        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
                <beans:prop key="cache.use_second_level_cache">true</beans:prop>
                <beans:prop key="cache.use_query_cache">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="LoginServiceImpl" class="com.tooltank.spring.service.LoginServiceImpl"/>

    <task:annotation-driven/>

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

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

    <cache:annotation-driven />

    <beans:bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <beans:property name="caches">
            <beans:set>
                <beans:bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                        p:name="person"/>
           </beans:set>
        </beans:property>
    </beans:bean>

    <!-- Configuration for Spring-Data-Redis -->
    <beans:bean id="jedisConnFactory"
                class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:usePool="true"/>

    <beans:bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="jedisConnFactory"/>


</beans:beans>

任何帮助都会很好。谢谢。

更新

GroupAttachmentsDAO:

public interface GroupAttachmentsDAO {

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    boolean addAttachment(GroupAttachments attachments, int noteid);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    List<GroupAttachments> searchInGroupAttachments(int noteid, String text);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    boolean removeAllAttachmentsForNote(int noteid);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    boolean removeAttachment(int attachId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    GroupAttachments getAttachmenById(int attachId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    List<GroupAttachments> getAttachmenByNoteId(int noteId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    int returnAttachmentCount(int noteId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    void deleteAttachmentsForGroup(Long groupId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    void deleteAttachmentsForSection(int sectionid, Long groupId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    void deleteAttachmentsForCanvas(int canvasid, Long groupId);

    @Secured({"ROLE_USER", "ROLE_ADMIN"})
    void deleteAttachmentsForNote(int noteid, Long groupAccountid);

}

1 个答案:

答案 0 :(得分:2)

如果您看到针对此特定异常的Spring Exception文档,则如下所示

public class BeanCurrentlyInCreationException
extends BeanCreationException
  

异常原因: - 在引用当前正在创建的bean的情况下抛出异常。通常在构造函数autowiring与当前构造的bean匹配时发生。

这意味着您在代码中编写的这行代码存在问题

private final GroupAttachmentsDAO groupAttachmentsDAO;
 @Autowired
    public GroupAttachmentsServiceImpl(GroupAttachmentsDAO groupAttachmentsDAO) {
        this.groupAttachmentsDAO = groupAttachmentsDAO;
    }

最好使用这段代码(删除final并尝试相同的代码)。

  private GroupAttachmentsDAO groupAttachmentsDAO; //remove final
     @Autowired
        public GroupAttachmentsServiceImpl(GroupAttachmentsDAO groupAttachmentsDAO) {
            this.groupAttachmentsDAO = groupAttachmentsDAO;
        }
  

因为字段上的@Autowiredfinal是矛盾的。