What is the Spring DI equivalent of CDI's InjectionPoint?

后端 未结 2 708
小鲜肉
小鲜肉 2020-12-15 22:43

I would like to create a Spring\'s bean producer method which is aware who invoked it, so I\'ve started with the following code:

@Configuration
public class          


        
2条回答
  •  温柔的废话
    2020-12-15 23:17

    Spring 4.3.0 enables InjectionPoint and DependencyDescriptor parameters for bean producing methods:

    @Configuration
    public class LoggerProvider {
    
        @Bean
        @Scope("prototype")
        public Logger produceLogger(InjectionPoint injectionPoint) {
            Class clazz = injectionPoint.getMember().getDeclaringClass();
    
            return LoggerFactory.getLogger(clazz);
        }
    }
    

    By the way, the issue for this feature SPR-14033 links to a comment on a blog post which links to this question.

提交回复
热议问题