简体   繁体   English

Spring Bean部分自动装配原型构造函数

[英]Spring bean partial autowire prototype constructor

I have a class: 我有一堂课:

@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 {

    }

}

and it attempts to create a prototype bean where 1 of the parameters is a runtime argument and the rest I want injected. 并尝试创建一个原型bean,其中的1个参数是运行时参数,其余的我想注入。 I need it to take the EventBus and a function. 我需要它来带EventBus和一个函数。 Both are available in the context and I can work past this problem. 两者都可以在上下文中使用,我可以解决这个问题。 I am trying to understand how I can do partial constructor autowiring on a prototype 我试图了解如何在原型上进行部分构造函数自动装配

@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) {

    }

}

I think you might need to rephrase your question, either ask a bout a very minimal example of what you want to achieve in Spring, or just ask about the pure Spring DI problem you are having. 我认为您可能需要重新表达您的问题,或者问一个关于您要在Spring中实现的目标的非常小的例子,或者只是询问您所遇到的纯粹Spring DI问题。 But anyway on the Spring Dependency Injection side it sounds like what you need is a Configuration, which you can create beans from: 但是无论如何,在Spring Dependency Injection方面,听起来您需要的是Configuration,您可以从以下位置创建bean:

@Configuration
public class TestConfiguration {

    @Bean
    @Autowired
    @Scope("prototype")
     public SomePrototypeBean createBean(SingletonBean singletonBean){
          return new SomePrototypeBean(singletonBean, "TODO whatever you want here" );
     }
}

This way you have greater control over the constructor arguments going into your bean, and can put in singleton beans in the constructor, and some sort of argument which changes at run-time, I left it as TODO. 这样,您可以更好地控制进入bean的构造函数参数,并且可以将单例bean放入构造函数中,并且可以在运行时更改某些类型的参数,我将其保留为TODO。 At all costs avoid context.getBean . 不惜一切代价避免使用context.getBean If you find yourself doing these sorts of things you may need to rethink about the way you are trying to achieve something. 如果您发现自己正在做这些事情,则可能需要重新考虑您尝试达成目标的方式。 It should only be used to overcome a limitation of the framework, if any other solution exists it is preferable. 它仅应用于克服框架的限制,如果存在任何其他解决方案,则它是可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM