Closed ChenZheOnePiece closed 4 years ago
Those MessageChannel
injections must be marked with the @Lazy
:
* <p>In addition to its role for component initialization, this annotation may also be placed
* on injection points marked with {@link org.springframework.beans.factory.annotation.Autowired}
* or {@link javax.inject.Inject}: In that context, it leads to the creation of a
* lazy-resolution proxy for all affected dependencies, as an alternative to using
* {@link org.springframework.beans.factory.ObjectFactory} or {@link javax.inject.Provider}.
It turns out that you have an injection in the same class where you provide beans for them, but since it is not direct bean creation, but rather late binding, those channels are not available as beans in early autowiring phase. So, postponing an interaction with field to the later action is the way to go for your sample.
Thank you very much. You solved my problem .It makes me very happy. I hope you are in good health i don't kown why Under spring-cloud-stream-samples , testing-demo why at the class can not use LAZY . I Think in the class add some Tips is better. This will save other people's time
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = "spring.cloud.stream.poller.fixed-delay=1")
@ImportAutoConfiguration(exclude = {
KafkaAutoConfiguration.class,
KafkaMetricsAutoConfiguration.class,
DataSourceAutoConfiguration.class,
TransactionAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class })
@DirtiesContext
class OddEvenSourceTests {
@Autowired
@Qualifier("oddEvenSupplier-out-0")
MessageChannel outputDestination;
@Autowired
MessageCollector collector;
@Test
void testMessages() {
BlockingQueue<Message<?>> messages = this.collector.forChannel(this.outputDestination);
assertThat(messages, receivesPayloadThat(is("odd")));
assertThat(messages, receivesPayloadThat(is("even")));
assertThat(messages, receivesPayloadThat(is("odd")));
assertThat(messages, receivesPayloadThat(is("even")));
}
}
It works in tests because the test configuration is populated in the end, when the application context is ready already. Therefore all the beans are available for autowiring.
thanks
I copy the following
but i can't Autowired this is my code