0

Is there any possibility to execute some code before every autowiring (for method and variable autowiring)? Because under some circumstances I want to add a primary component before this autowire/injection happened.

I have different services implementing the same interface (business implementation, rest client, ...). Now it depends on several circumstances which implementation should be used. Because of this I implemented a proxy service (annotated with @Primary) which is created dynamically. At the moment at this is done at "BeanDefinitionRegistryPostProcessor" - but I need to scan for all components and this take some time.

I want to avoid this and want to create this proxy dynamically - I tried to add the proxy at "postProcessBeforeInitialization". But there is a problem if a bean should be injected which is not already created - because there are more than one possible instance and none of them is @Primary (because without initialization of this component my proxy is missing at this moment of autowiring).

5
  • 1
    I suppose it's possible if you use method injection. But this sounds more like something that should be handled on the configuration side. For example, could you add this primary component as an injected dependency instead? Commented Oct 17, 2023 at 8:47
  • Now it depends on several circumstances, feels like instead of adding a proxy and do much of this complexity why not write your own conditionals to select the implementation instead of a proxy. Only create the instance you need. Commented Oct 17, 2023 at 9:12
  • @Jorn : Thanks for your answer. Yes, at the moment I already do the scan at configuration phase. Even the creation and the insertion of the primary proxy beans. This works, but I want to do this on the fly (avoid scanning) - but at the moment I think there is possibility to solve this after configuration phase. Commented Oct 23, 2023 at 9:06
  • @M. Deinum : Thanks for your answer. This is a very dynamic application and the proxy instance decide which remote server should be requested (or maybe the local service if no remote is present and the service exists locally). Because of this all beans needs to be there and I just select the correct bean (and configure this) during the request. Maybe the next request will address a different server (because of round-robin, load-balancing or just because one of the remotes is down). It's a very nice idea to use conditionals, but I think this will not solve my problem. Commented Oct 23, 2023 at 9:11
  • Then a proxy won't help you either, you will need to implement this logic somewhete and instead of hiding it in annotations, reflections/aop just write it in a class that does what you want. Commented Oct 24, 2023 at 6:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.