早上踩到的坑,有必要记录下!
问题:spring整合websocket注入service失败
解决办法:
第一步 :在pom.xml文件中添加 jar 包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
第二步 :在服务端添加configurator = SpringConfigurator.class
@ServerEndpoint(value = "/api/websocket", configurator = SpringConfigurator.class)
@Component
public class WebSocketService {
@Autowired
private UserService userService;
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>