【发布时间】:2016-10-25 00:49:54
【问题描述】:
我有一堂课:
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WebSocketRegistrar extends AbstractWebSocketHandler{
private final ApplicationContext context;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// problem here.
context.getBean(WebSocketConsumer.class, session);
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
}
}
它尝试创建一个原型 bean,其中 1 个参数是运行时参数,其余的我想注入。我需要它来获取 EventBus 和一个函数。两者都在上下文中可用,我可以解决这个问题。我试图了解如何在原型上进行部分构造函数自动装配
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@NoArgsConstructor
public class WebSocketConsumer implements Consumer<Identifiable<?>> {
private WebSocketSession session;
private Function<Identifiable<?>, String> jsonApi;
@Autowired
public WebSocketConsumer(WebSocketSession session, EventBus bus, BiFunction<Identifiable<?>, String, String> toJsonApi) {
this.session = session;
this.jsonApi = identifiable -> toJsonApi.apply(identifiable,session.getHandshakeHeaders().get("Host").get(0));
bus.on(R(session.getUri().getPath() + "/?.*"),this::accept);
}
@Override
public void accept(Identifiable<?> update) {
}
}
【问题讨论】: