Java 无法自动装配。找不到 SimpMessagingTemplate 类型的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22925951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not autowire. No beans of SimpMessagingTemplate type found
提问by Tk421
I am configuring Websockets in Spring basically by following the guide provided in the documentation.
我基本上按照文档中提供的指南在 Spring 中配置 Websockets。
I am currently trying to send a message from the server to the client as explained in the section "Sending messages from anywhere"
我目前正在尝试从服务器向客户端发送消息,如“从任何地方发送消息”部分所述
Following the example, you can Autowire a class called SimpMessagingTemplate
按照示例,您可以自动装配一个名为 SimpMessagingTemplate 的类
@Controller
public class GreetingController {
private SimpMessagingTemplate template;
@Autowired
public GreetingController(SimpMessagingTemplate template) {
this.template = template;
}
@RequestMapping(value="/greetings", method=POST)
public void greet(String greeting) {
String text = "[" + getTimestamp() + "]:" + greeting;
this.template.convertAndSend("/topic/greetings", text);
}
}
However, my current project cannot find the bean "SimpMessagingTemplate". (Intellij: 'Could not autowire. No beans of SimpMessagingTemplate type found'.
但是,我当前的项目找不到 bean“SimpMessagingTemplate”。(Intellij:'无法自动装配。找不到 SimpMessagingTemplate 类型的 bean'。
I have check several examples in the internet but I cannot find how to get Spring to create an instance of SimpMessagingTemplate. How can I Autowire it ?
我在互联网上检查了几个例子,但我找不到如何让 Spring 创建 SimpMessagingTemplate 的实例。我怎样才能自动装配它?
EDIT:
编辑:
I decided to send some more background information. This is my current websocket configuration:
我决定发送更多背景信息。这是我当前的 websocket 配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
<!-- TODO properties to be read from a properties file -->
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/new_session" >
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
</beans>
Websocket works with this controller
Websocket 与此控制器配合使用
@Controller
public class SessionController {
private static final Logger log = LoggerFactory.getLogger(SessionController.class);
@MessageMapping("/new_session")
@SendTo("/topic/session")
public SessionStatus newSession(Session session) throws Exception {
Thread.sleep(3000); // simulated delay
log.info("Response sent !!");
return new SessionStatus("StatusReport, " + session.toString() + "!");
}
}
I just not sure how to to make this work
我只是不知道如何使这项工作
public class SessionController {
private static final Logger log = LoggerFactory.getLogger(SessionController.class);
private SimpMessagingTemplate template;
@Autowired
public SessionController(SimpMessagingTemplate template) {
this.template = template;
}
}
As the bean "SimpMessagingTemplate template" is not found. Spring documentation does not offer more details regarding this matter.
由于未找到 bean“SimpMessagingTemplate 模板”。Spring 文档没有提供有关此问题的更多详细信息。
EDIT: Example of working code in github
编辑:github 中的工作代码示例
采纳答案by Rossen Stoyanchev
Strange, because when you use the websocket namespace, the "message-broker" element causes the creation of a SimpMessagingTemplate bean which should then be available for you to inject. Are both the controller and the websocket namespace in the same ApplicationContext or is perhaps one in the "root" context and the other in the DispatcherServlet context?
奇怪,因为当您使用 websocket 命名空间时,“message-broker”元素会导致创建一个 SimpMessagingTemplate bean,然后您应该可以注入该 bean。控制器和 websocket 命名空间是否在同一个 ApplicationContext 中,或者一个在“root”上下文中,另一个在 DispatcherServlet 上下文中?
回答by earthling
You shall either have a bean definition id with same name as class name in your applicationContext xml or annotate @Component on injecting class for Autowire to work
您应该在 applicationContext xml 中有一个与类名同名的 bean 定义 id,或者在注入类时注释 @Component 以便 Autowire 工作
<bean id="SimpMessagingTemplate " class="your-class" >
You may need to define below tag pointing to your package for later case
您可能需要定义以下标记指向您的包以备后用
<context:component-scan base-package="com.x.y"/>
回答by raymond
Rossen was correct. The addition of the element will add the SimpMessagingTemplate bean to the context for injection. This has to be in the web app root context, not the context for Spring DispatchServlet. E.g., in the following web.xmlfile, the message-broker element should be included in app-root.xml file. Including only in app-mvc.xml will cause NoSuchBeanDefinitionException.
罗森是对的。添加元素会将 SimpMessagingTemplate bean 添加到上下文以进行注入。这必须在 Web 应用程序根上下文中,而不是 Spring DispatchServlet 的上下文中。例如,在以下web.xml文件中,消息代理元素应包含在 app-root.xml 文件中。仅在 app-mvc.xml 中包含将导致 NoSuchBeanDefinitionException。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/app-root.xml</param-value>
</context-param>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/app-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
回答by Urbanleg
I had the same problem, the error occurred because my websocket config file:
我遇到了同样的问题,发生错误是因为我的 websocket 配置文件:
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
}
was not scanned by spring.
没有被春天扫描。
so the fix was to add the package with this configuration file to the scanned packages.
所以修复是将带有此配置文件的包添加到扫描的包中。