A better way to construct autowired spring vaadin components?

I’ve got a bunch of places in my application where I want to autowire prototype-scope objects that are both Vaadin components and Spring components. I have is that I’ve got a bunch of legacy code that uses a service that is ApplicationContextAware which saves off a copy of the context’s bean factory to use to construct the instances I need. The problem is that static bean factory doesn’t seem to be the same instance that I get if I make a regular application Component ApplicationContextAware: it works, but it is obviously using a different bean cache since it takes a lot longer to find beans initially. To be clear, I need to be able to construct these prototype scoped beans on the fly, not via standard declarative injection, but by doing something like ApplicationContext.getbean() or Instantiator.getOrCreate()

Edit: Vaadin 23.3, flow

I’m not sure if it answers your question but sometimes I’m using the object provider to create spring bean. java - How to use Spring ObjectProvider with more than one bean definition - Stack Overflow

Unfortunately, not really. In my situation, the problem isn’t which time to instantiate but how to parameterize it. Example: a custom extension of grid that takes both a set of rows to render (e.g. as a Query or a List) and spring injected helper services to implement the column renderers. Yeah, I can just new such an object with explicit constructor arguments for both the services to inject and the specific data model for this instance, but I’d rather use ApplicationContext.getBean(MyGrid.class, myDataModel) and let spring inject all the services it needs.

How about injecting a factory bean into the view? And using that bean for creating beans?

Yeah, that’s pretty close to what I’m doing, but it feels like it is overly complex/fragile. The best solution I’ve come up with is to have an appropriately scoped factory service that is ApplicationContextAware, which then makes the beans for me. The beans themselves get created with ApplicationContext.getBean(clazz, args…), with constructor arguments passed and autowired members. Of course, I can do the same thing without the factory by just using ApplicationContextAware directly, but that feels like mixing too many metaphors.

I agree. Bringing getBean in may make unit testing harder.