Spring Cloud Stream和@Publisher注释的兼容性 [英] Spring Cloud Stream and @Publisher annotation compatiblity
问题描述
由于Spring Cloud Stream没有用于向流发送新消息的注释(@SendTo仅在声明@StreamListener时有效),因此我尝试为此目的使用Spring Integration注释,即@Publisher.
Since Spring Cloud Stream has not an annotation for sending a new message to a stream (@SendTo only works when @StreamListener is declared), I tried to use Spring Integration annotation for that purpose, that is @Publisher.
由于@Publisher需要一个频道,并且Spring Cloud Stream的@EnableBinding批注可以使用@Output批注绑定输出频道,因此我尝试通过以下方式对其进行混合:
Because @Publisher takes a channel and @EnableBinding annotations of Spring Cloud Stream can bind an output channel using @Output annotation, I tried to mix them in the following way:
@EnableBinding(MessageSource.class)
@Service
public class ExampleService {
@Publisher(channel = MessageSource.OUTPUT)
public String sendMessage(String message){
return message;
}
}
此外,我在配置文件中声明了@EnablePublisher批注:
Also, I declared @EnablePublisher annotation in a configuration file:
@SpringBootApplication
@EnablePublisher("")
public class ExampleApplication {
public static void main(String[] args){
SpringApplication.run(ExampleApplication.class, args);
}
}
我的测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleServiceTest {
@Autowired
private ExampleService exampleService;
@Test
public void testQueue(){
exampleService.queue("Hi!");
System.out.println("Ready!");
}
}
但是我遇到了以下错误:
But I'm getting the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.ExampleServiceTest': Unsatisfied dependency expressed through field 'exampleService'; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'exampleService' is expected to be of type 'com.example.ExampleService' but was actually of type 'com.sun.proxy.$Proxy86'
这里的问题是不能插入ExampleService bean.
Problem here is that ExampleService bean can not be injected.
任何人都知道我该如何做这项工作?
Anyone knows how can I make this work?
谢谢!
推荐答案
由于在ExampleService
中使用了@Publisher
批注,因此该批注将作为该发布内容的代理.
Since you use a @Publisher
annotation in your ExampleService
, it is proxied for that publishing stuff.
解决该问题的唯一方法是公开ExampleService
的接口,并将该接口注入到您的测试类中:
Only the way to overcome the issue is to expose an interface for your ExampleService
and inject already that one into your test class:
public interface ExampleServiceInterface {
String sendMessage(String message);
}
...
public class ExampleService implements ExampleServiceInterface {
...
@Autowired
private ExampleServiceInterface exampleService;
另一方面,您的ExampleService.sendMessage()
似乎不对消息进行任何操作,因此您可以考虑在某些界面上使用@MessagingGateway
:
On the other hand it looks like your ExampleService.sendMessage()
does nothing with the message, so you may consider to use a @MessagingGateway
on some interface instead: https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#gateway
这篇关于Spring Cloud Stream和@Publisher注释的兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!