I have a websocket scoped bean that do something everytime that a new session is created. The problem is that this bean use some observable handlers, it's not called directly, so the bean is never instantiated.
How do I instantiate a websocket scoped bean without making a direct acces to it?
My solution:
Create an interface to use on the components that you want to be initialized:
public interface WebSocketSessionPrematureInitialization {
default void initialize() {}
}
Create a component to initialize all the components that implements the interface:
@Component
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class WebSocketBeanInitializer {
private final List<? extends WebSocketSessionPrematureInitialization> wsInit;
@Autowired
public WebSocketBeanInitializer(List<? extends WebSocketSessionPrematureInitialization> wsInit) {
this.wsInit = wsInit;
}
public void initialize() {
for (WebSocketSessionPrematureInitialization bean : wsInit)
bean.initialize();
}
}
Now you just need to call the initialize() method of the WebSocketBeanInitializer always that a new session is created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.