Spring Bean Alias in JavaConfig

前端 未结 3 1936
一整个雨季
一整个雨季 2021-01-11 18:50

I have a @Service annotated class which provides core functionality which I can use in all my projects:

@Service
public class MyService {}
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-11 19:29

    If you want to use the placeholder, another workaround is to use @Bean in a @Configuration class using @Value and the Spring applicationContext.

    @Configuration
    public class AppConfig {
    
        @Autowired
        private ApplicationContext context;
    
        @Bean
        public MyService myService(@Value("${myService.qualifier}") String qualifier) {
            return (MyService) context.getBean(qualifier);
        }
    }
    

    NB : special consideration must be taken for the placeholder bean which must be loaded at the beginning (cf javadoc)

提交回复
热议问题