2016-06-21 157 views
0

我正在开发Spring MVC应用程序,其中我有一个服务图层类,其中用于转换文档我想使用@Async注释。不幸的是,当我添加异步注释时,项目无法启动,并且我得到一个当前处于创建异常的bean。Spring:bean添加@Async注释后目前处于创建错误状态

错误日志:

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

如果你看到Spring是异常的文档,这个特殊的例外是如下

public class BeanCurrentlyInCreationException 
extends BeanCreationException 

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

这意味着这行代码是你写在你的代码

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

更好地使用这段代码(除去最后并尝试相同的代码)的问题。

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

因为有一个字段@Autowiredfinal是矛盾的。

+0

由于您提出的修改,我得到一个错误,groupAttachmentsDAO可能尚未初始化。如果我删除了最终版,那么我仍然会像以前一样得到相同的错误。有什么想法吗? –

+0

另外,GroupAttachmentsDAO是一个接口.... –

+0

是你的类'GroupAttachmentsDAO'被配置为'Resource'或'service'或'bean'吗?我在评论中提到了.. 请发布类“GroupAttachmentsDAO”的定义。这可能会帮助我确定问题。 谢谢.. –