1

I'm developing a framework in Spring boot, that is used by some applications. In some point at runtime, the application supply me (the framework) an instance of class A, that I want to aspect (using spring) its func method.
The problem is, that I am not the one who construct this instace so I can't create it by applicationContext.getBean(A.class).
Is there a way to give Spring this instance and tell it to make it (or proxy it with) a bean?

public interface A {
    void func(int i);
}

Aspect:

@Aspect
public class AspectClass {

   @Around("execution(*com.A.func(..))")
   public void funcAspect(ProceedingJoinPoint joinPoint) throws Throwable {
        ....
   }
}

I expect it to be something like: A bean = applicationContext.makeBean(instanceOfA)

EDIT: I can't control the construction of A, I'm not the one who construct it, and I just get its existing instance and want to aspect its func method, from now on.

Harel
  • 36
  • 6
  • Search first, ask second. – Michael Mar 27 '19 at 15:34
  • It seems like the referenced-question is about adding a new bean definition and not about making an existing instance a bean. I edit the question to be more clear. – Harel Mar 28 '19 at 14:06
  • @Michael, I will be glad if you reopen the question, or explain me how the referenced question answer mine. – Harel Apr 01 '19 at 19:30
  • Well, explain how it doesn't – Michael Apr 01 '19 at 21:58
  • @Michael, see my above comment and the clarification I added to the question – Harel Apr 02 '19 at 04:04
  • I **have** read your edit. The linked duplicate answers your question. `registry.registerBeanDefinition(beanId, newBeanObj)`. `newBeanObj` being the instance you already have. – Michael Apr 02 '19 at 09:10
  • @Michael, newBeanObj should be of type [BeanDefinition](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/config/BeanDefinition.html). How it can be my instance that is of type A? – Harel Apr 03 '19 at 18:43
  • @Michael, can you help me? – Harel Apr 12 '19 at 06:03
  • 2
    @Micheal The linked issue is not relevant for this question. Adding an existing instance of an object is not the same as adding a BeanDefinition. The referenced question is talking about adding a BeanDefinition. If you can reopen the question I can add an answer in the proper form. In short you have to. ((SingletonBeanRegistry)applicationContext.getAutowireCapableBeanFactory()).registerSingleton("MyInstance", objectInstance); – Navid Mitchell Mar 19 '20 at 20:51
  • Even though the referenced question has nothing to do with this one, it does provide a solution that just so happens to address this question, see https://stackoverflow.com/a/63396727/2194119. In addition to that you can also use `BeanDefinition.setInstanceSupplier` (Not sure the pros and cons). @Harel, I'm sorry your question was closed. The title of the question is very much to the point (I needed the same functionality and found this question and not the referenced one). Had the question been open, I'd have posted the solution as an answer. – Rad Aug 18 '23 at 11:50

1 Answers1

0

Are you aware of @Bean annotation in Java @Configuration class? this is the 3rd and popular way of creating beans and that's exactly for the moment, when you don't want your Spring container to instantiate default beans from the candidate classes, but rather create your custom object (in the @Bean method), make any adjustments to that object (before you return it) and finally return that instance. Spring will automatically register that (your custom object) object as a bean in its application context.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • as I metioned, I have no control on the instance creation, the application create it and supply it to the framework – Harel Mar 27 '19 at 15:53
  • That's what I say, that you can take this control by registering the bean not from @Autowired or XML bean definition, but from Java source code - namely `@Configuration` + `@Bean`. – Giorgi Tsiklauri Mar 27 '19 at 16:47
  • I can't change the API, the application supply me the instance and i have no control on creating it – Harel Mar 28 '19 at 14:02