简体   繁体   English

JavaConfig中的Spring Bean别名

[英]Spring Bean Alias in JavaConfig

I have a @Service annotated class which provides core functionality which I can use in all my projects: 我有一个@Service注释类,它提供了我可以在我所有项目中使用的核心功能:

@Service
public class MyService {}

and another one which extends it to implement project specific stuff: 和另一个扩展它以实现项目特定的东西:

@Service
public class ExtendedMyService extends MyService {}

Now I would like to configure a bean alias to be able to use @Qualifier("MyServiceAlias") when autowiring it using a property: 现在我想配置一个bean别名,以便在使用属性自动装配它时能够使用@Qualifier("MyServiceAlias")

# MyService qualifier (default: myService)
myService.qualifier=extendedMyService

In XML it would look like: 在XML中它看起来像:

<alias name="${myService.qualifier}" alias="MyServiceAlias" />

It is also discussed here , but I need to do it w/o XML, JavaConfig only. 这里也讨论了它,但我只需要使用XML,JavaConfig。 Is it possible and how to realize? 是否可能以及如何实现?

There is an open Jira for this: https://jira.spring.io/browse/SPR-6736 有一个开放的Jira: https//jira.spring.io/browse/SPR-6736

The workaround is to use @Bean in @Configuration class: 解决方法是在@Configuration类中使用@Bean

@Configuration
public class AppConfig {

  @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
  public MyService myService() {}

}

If you want to use the placeholder, another workaround is to use @Bean in a @Configuration class using @Value and the Spring applicationContext. 如果要使用占位符,另一种解决方法是使用@Value和Spring applicationContext在@Configuration类中使用@Bean。

@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 ) 注意:必须特别考虑必须在开头加载的占位符bean(cf javadoc

With small amount of configuration and one ImportBeanDefinitionRegistrar you can configure bean aliases via Java configuration. 使用少量配置和一个ImportBeanDefinitionRegistrar您可以通过Java配置配置bean别名。 You can check bean-alias library project for reference - developed for the needs of my projects. 您可以检查bean-alias库项目以供参考 - 根据我的项目需求而开发。 Feel free to modify and/or copy the source into your own project in case the spring version used in it does not work with your setup. 如果源中使用的弹簧版本不适用于您的设置,请随意修改和/或将源复制到您自己的项目中。

Once you have the library on your path, you declare an alias through the annotation: 在路径上有库后,通过注释声明别名:

@Configuration
@BeanAlias(name = "fromName", alias = "toName")
public class ExampleConfiguration {
}

That's it. 而已。

How it works is that with the annotation we import a ImportBeanDefinitionRegistrar implementation 它的工作原理是使用注释我们导入ImportBeanDefinitionRegistrar实现

@Import(BeanAliasBeanRegistrar.class)
public @interface BeanAlias {
}

which registers the alias in the BeanDefinitionRegistry 它在BeanDefinitionRegistry注册别名

class BeanAliasBeanRegistrar implements ImportBeanDefinitionRegistrar, PriorityOrdered {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
      ...
      registerAlias(registry, metadata.getAnnotationAttributes(BeanAlias.class.getName()));
    }

    private void registerAlias(BeanDefinitionRegistry registry, Map<String, Object> attributes) {
      ...
      registry.registerAlias(name, alias);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM