2017-10-10 1 views
0

J'ai un exemple assez minime, qui provoque une erreur. Notez que les paquets semblent importer, en mettant toutes les classes dans le même paquet fonctionne.Pourquoi un simple exemple de démarrage/intégration de Spring cause-t-il que "Le bean demandé est actuellement en cours de création"?

Fondamentalement, je crée un DirectChannel, j'ai un @Component qui est automatiquement câblé avec lui, et un ServiceActivator dans la @Configuration qui fait référence au même canal. Pour autant que je sache, je ne fais rien de spécial ou d'inhabituel, alors peut-être que c'est un bug?

exemple de code complet en dessous, prêter attention aux paquets ('test' et 'test.config')

Merci!

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.7.RELEASE</version> 
    </parent> 

    <groupId>my.test</groupId> 
    <artifactId>Circularity</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <!-- Spring boot integration starter --> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-integration</artifactId> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <!-- Package as an executable jar --> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

IntegrationConfiguration.java

package test; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.integration.annotation.ServiceActivator; 
import org.springframework.integration.channel.DirectChannel; 
import org.springframework.messaging.MessageHandler; 

@Configuration 
public class IntegrationConfiguration { 

    @Bean 
    DirectChannel myChannel() { 

     return new DirectChannel(); 
    } 

    @Bean 
    @ServiceActivator(inputChannel = "myChannel") 
    public MessageHandler stdoutMessageHandler() { 

     return m -> System.out.println(m.toString()); 
    } 

} 

SomeComponent.java

package test.config; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.messaging.MessageChannel; 
import org.springframework.stereotype.Component; 

@Component 
public class SomeComponent { 

    @Autowired 
    public SomeComponent(MessageChannel myChannel) { 
     // Pretend I store the MessageChannel so I can send something to it later. 
     // But this is not needed to cause the issue! 
    } 

} 

CircularApp.java

package test; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class CircularApp { 

    public static void main(String[] args) { 

     SpringApplication.run(CircularApp.class, args); 
    } 
} 

Et sur la demande populaire, une trace de la pile :) Notez que c'est sur une autre machine, avec le code de cette question rapidement copié collé.

. ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.7.RELEASE) 

2017-10-10 20:14:26.537 INFO 1796 --- [   main] test.CircularApp       : Starting CircularApp on ORCA with PID 1796 (C:\Storage\workspace\Circularity\target\classes started by Jeroen in C:\Storage\workspace\Circularity) 
2017-10-10 20:14:26.539 INFO 1796 --- [   main] test.CircularApp       : No active profile set, falling back to default profiles: default 
2017-10-10 20:14:26.578 INFO 1796 --- [   main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]7dc222ae: startup date [Tue Oct 10 20:14:26 CEST 2017]; root of context hierarchy 
2017-10-10 20:14:26.790 INFO 1796 --- [   main] o.s.i.config.IntegrationRegistrar  : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created. 
2017-10-10 20:14:26.877 INFO 1796 --- [   main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created. 
2017-10-10 20:14:26.879 INFO 1796 --- [   main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created. 
2017-10-10 20:14:26.960 INFO 1796 --- [   main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
2017-10-10 20:14:26.963 INFO 1796 --- [   main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [java.util.Properties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
2017-10-10 20:14:27.028 WARN 1796 --- [   main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someComponent' defined in file [C:\Storage\workspace\Circularity\target\classes\test\config\SomeComponent.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationConfiguration' defined in file [C:\Storage\workspace\Circularity\target\classes\test\IntegrationConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.messaging.core.DestinationResolutionException: A bean definition with name 'myChannel' exists, but failed to be created; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myChannel': Requested bean is currently in creation: Is there an unresolvable circular reference? 
2017-10-10 20:14:27.034 INFO 1796 --- [   main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 
2017-10-10 20:14:27.039 ERROR 1796 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someComponent' defined in file [C:\Storage\workspace\Circularity\target\classes\test\config\SomeComponent.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationConfiguration' defined in file [C:\Storage\workspace\Circularity\target\classes\test\IntegrationConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.messaging.core.DestinationResolutionException: A bean definition with name 'myChannel' exists, but failed to be created; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myChannel': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
    at test.CircularApp.main(CircularApp.java:11) [classes/:na] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationConfiguration' defined in file [C:\Storage\workspace\Circularity\target\classes\test\IntegrationConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.messaging.core.DestinationResolutionException: A bean definition with name 'myChannel' exists, but failed to be created; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myChannel': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    ... 18 common frames omitted 
Caused by: org.springframework.messaging.core.DestinationResolutionException: A bean definition with name 'myChannel' exists, but failed to be created; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myChannel': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:93) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:46) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.createEndpoint(AbstractMethodAnnotationPostProcessor.java:290) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.postProcess(AbstractMethodAnnotationPostProcessor.java:220) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.processAnnotationTypeOnMethod(MessagingAnnotationPostProcessor.java:228) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor$1.doWith(MessagingAnnotationPostProcessor.java:200) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:530) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:537) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:180) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    ... 37 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myChannel': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:89) ~[spring-integration-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    ... 48 common frames omitted 

Répondre

0
@Autowired 
public SomeComponent(MessageChannel myChannel) { 
    // Pretend I will send something to the channel. 
} 

Vous ne pouvez pas commencer à envoyer des messages jusqu'à ce que le contexte d'application est entièrement construit; envoyer un message d'un constructeur est évidemment avant cela.

+0

Droit, mis à jour la question pour mieux refléter mon cas :) Dans le plus grand code dont il est dérivé, le constructeur stocke juste le MessageChannel pour une utilisation ultérieure. Notez également que l'utilisation d'un setter a exactement le même effet et que ce code tel que publié échoue, même s'il n'utilise pas réellement MessageChannel dans le constructeur. – cranphin

+0

Vous devez afficher une trace de pile. –

+0

Une trace de pile est ajoutée! :) – cranphin