WebSocket @Autowired
반응형
`@SeverEndPoint`가 있는 컨트롤러에서 `@Autowired` 사용 시 오류 발생 시 해결 방법
[Error 로그]
ERROR [2022-09-12 22:31:49,989] [nio-8080-exec-8] o.a.coyote.http11.Http11NioProtocol :175 - Error reading request, ignored
java.lang.IllegalArgumentException: javax.websocket.DeploymentException: Endpoint instance creation failed
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:139)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:933)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1787)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: javax.websocket.DeploymentException: Endpoint instance creation failed
at org.apache.tomcat.websocket.WsSession.<init>(WsSession.java:277)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:127)
... 7 common frames omitted
Caused by: java.lang.NoSuchMethodException: us.puter.park.controller.WebSocketController.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3349)
at java.base/java.lang.Class.getConstructor(Class.java:2151)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142)
at org.apache.tomcat.websocket.WsSession.<init>(WsSession.java:274)
... 8 common frames omitted
[ServerEndPointConfig 설정 추가]
package us.puter.park.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServerEndpointConfig extends javax.websocket.server.ServerEndpointConfig.Configurator implements ApplicationContextAware {
private static volatile BeanFactory context;
@Override
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
return context.getBean(clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ServerEndpointConfig.context = applicationContext;
}
}
[websocket 컨트롤러 수정]
`@ServerEndpoint(value = "/ws/chat/{nickname}", configurator = ServerEndpointConfig.class)`
- `@ServerEndPoint`로 어노테이션이 달린 클래스는 WebSocket이 연결될 때마다 인스턴스가 생성되고 JWA구현에 의해 관리가 되어 내부의 `@Autowired`가 설정된 멤버가 정상적으로 초기화 되지 않는다.
- `@Autowired`를 사용하기 위해서 `ServerEndpointConfig.Configurator`를 상속받아 `ServerEndPoint`의 컨텍스트에 `BeanFactory` 또는 `ApplicationContext`를 연결해 주는 작업을 하는 설정을 추가해야 한다.
반응형
'development' 카테고리의 다른 글
centos8에서 mariadb 설치 rpm 문제 (0) | 2023.05.01 |
---|---|
UnsatisfiedDependency (0) | 2022.09.15 |
lombok annotation (0) | 2022.09.12 |
spring boot transaction (0) | 2022.09.08 |
PostgreSQL 리눅스 설치 (0) | 2021.04.29 |
댓글
이 글 공유하기
다른 글
-
centos8에서 mariadb 설치 rpm 문제
centos8에서 mariadb 설치 rpm 문제
2023.05.01 -
UnsatisfiedDependency
UnsatisfiedDependency
2022.09.15 -
lombok annotation
lombok annotation
2022.09.12 -
spring boot transaction
spring boot transaction
2022.09.08