java - 依赖注入不适用于@ClientEndpoint java
问题描述
有没有办法在这个 @ClientEndpoint 类中启用 cdi(仍然使用注释而不是编程端点类)?我正在使用 Wildfly 14 和 java 8。
这是创建会话的代码,将类名传递给“createConnection”方法:
@ApplicationScoped //TODO move this to be request scoped
public class SessionProducer {
@Produces
public Session getSession(InjectionPoint ip) {
SessionAnnotation annotation = ip.getAnnotated().getAnnotation(SessionAnnotation.class);
if(annotation != null) {
Class clazz = annotation.clazz();
String url = annotation.serverURL();
WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
try {
return webSocketContainer.connectToServer(clazz, new URI(url)); //<----------this is the line that uses the annotated class (clazz is a reference to the class)
} catch (DeploymentException | IOException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* The destroy/disposer metho for the session
* @param session
*/
public void close(@Disposes Session session) {
try {
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是带注释的端点类:
@ClientEndpoint
public class CryptoCompareWSClient {
@Inject
@CryptoCompare
private Event<String> cryptoCompareEvent; //<--------this is always null, no cdi happens
public CryptoCompareWSClient() {
System.out.println("constructor");
//cryptoCompareEvent = new Event();
}
@PostConstruct
public void init() {
System.out.println("post construct"); //<---------this never gets called
}
@OnOpen
public void open(Session session) {
//session.getAsyncRemote().sendText("SubAdd: { subs: ['0~Poloniex~BTC~USD'] }" /*"test"*/);
System.out.println("opened");
}
@OnClose
public void close(Session session) {
System.out.println("Session " + session + " closed");
}
@OnError
public void error(Throwable error) {
System.out.println("Error: " + error.getMessage());
}
@OnMessage
public void message(String message, Session session) {
System.out.println("Message");
//cryptoCompareEvent.fireAsync(message);
}
}
有什么方法可以在启用的类中启用 cdi 吗?
谢谢。
解决方案
推荐阅读
- visual-studio-code - 如何在 Visual Studio 代码中打开 zip 文件夹
- javascript - 如何按日期对 DataTable 列进行排序
- laravel - Laravel Passport 以编程方式创建客户端
- java - Spring Boot:配置 h2 jdbc url
- javascript - 如何获取 Alpine.js 组件中数据属性的值?
- c# - Graphics.MeasureString 不包含某些字符的某些部分
- python - 通过单击单选按钮禁用输入字段
- laravel - Laravel 控制器中的调用事件
- amazon-dynamodb - 从 JSON 或 CSV 将 50K 记录加载到 DynamoDB
- sql - POSTGRES - 使用 ON CONFLICT DO NOTHING 防止串行增量