Class Dependency Injection that I don’t have access to

Asked

Viewed 33 times

0

Hello, how are you?

How can I inject dependency into a class I don’t have access to, that is, I can’t say on top of the class that it’s a java bean (@Component).

Thank you very much!

  • Thank you for the answer, Piovezan. My question was not well worded. Excuse me. The impasse I’m in is that I have a Java file that I don’t have access to. And I would like to brand it as @Component, so that I can do dependency injection. Is there any way to do that? Thank you very much!

1 answer

1


You can do this through Applicationcontext using the getAutowireCapableBeanFactory() together with autowireBeanProperties():

@Autowired
ApplicationContext ctx;

private void foo(){
    ClasseQueNaoTemAcesso umaClasseQueNaoTenhoAcesso = new ClasseQueNaoTemAcesso();
    ctx.getAutowireCapableBeanFactory()
    .autowireBeanProperties(umaClasseQueNaoTenhoAcesso,
    AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
}
  • Thank you very much, Brenno!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.