I've my Logger bean configured as below, for some specific requirements I need to access the Logger into some of the classes that are not managed by Spring. I've done the following steps to do so.

@Bean
@Scope("prototype")
Logger logger(InjectionPoint injectionPoint){
  return LoggerFactory.getLogger(injectionPoint.getMethodParameter().getContainingClass());
}

I'm trying to inject the logger bean into a non-spring class using the below line.

LOGGER = SpringApplicationContext.getBean(Logger.class);

And my SpringApplicationContext class is below

@Component
public class SpringApplicationContext implements ApplicationContextAware {

    private static ApplicationContext context;

    public static <T extends Object> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        // store ApplicationContext reference to access required beans later on
        SpringApplicationContext.context = context;
    }
}

Since the Logger is a prototype with InjectionPoint argument, I'm getting this error No current InjectionPoint available for method 'logger' parameter 0.

Can someone help to fix this or is there a better way to inject prototype beans with InjectionPoint argument into non-spring classes??

Comment From: wilkinsona

@sekkhar17 Please don't duplicate comments across issues. Each one notifies everyone watching the repository.

Duplicate of https://github.com/spring-projects/spring-boot/issues/8106#issuecomment-951070324.

Comment From: sekkhar17

@wilkinsona: Apologies for the inconvenience. I've removed the duplicate from another place, could you please mark this as waiting for triage.

Comment From: wilkinsona

You can't use InjectionPoint when you're looking up a bean yourself. When you do that, there's no dependency injection taking please so InjectionPoint is unavailable.

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.