[英]Why do we use a qualifier when we can have a name for the bean?
Why do we use qualifiers with @Bean when we can have different names for different beans of the same type (class)?当我们可以为相同类型(类)的不同 bean 使用不同的名称时,为什么我们要对 @Bean 使用限定符?
@Bean
@Qualifier("fooConfig")
public Baz method1() {
}
Isn't the following code more clean?下面的代码不是更干净吗?
@Bean("fooConfig")
public Baz method1() {
}
If I create two beans of the same type with different names (using @Bean annotation), then can we inject them specifically using the @Qualifier annotation(can be added on field/constructor parameter/setter) in another bean?如果我创建两个具有不同名称的相同类型的 bean(使用 @Bean 注释),那么我们可以使用 @Qualifier 注释(可以添加到字段/构造函数参数/setter 上)专门将它们注入另一个 bean 中吗?
@Bean("fooConfig")
public Baz method1(){
}
@Bean("barConfig")
public Baz method2(){
}
// constructor parameter of a different bean
final @Qualifier("fooConfig") Baz myConfig
If the above is true, then where do we use @Qualifier (with @Bean or @Component) instead of giving the bean a name as shown below?如果以上是正确的,那么我们在哪里使用@Qualifier(与@Bean 或@Component)而不是给bean 命名,如下所示?
@Bean
@Qualifier("fooConfig")
public Baz method1(){
}
@Bean
@Qualifier("barConfig")
public Baz method2(){
}
// constructor parameter of a different bean
final @Qualifier("fooConfig") Baz myConfig
For example:例如:
@Configuration
public class MyConfiguration {
@Bean
public MyCustomComponent myComponent() {
return new MyCustomComponent();
}
}
If you will try to inject myComponent somewhere, Spring is smart enough to find the bean above.如果您将尝试在某处注入 myComponent,Spring 足够聪明,可以找到上面的 bean。 Becaude there is only one Bean with return type MyCustomComponent.因为只有一个返回类型为 MyCustomComponent 的 Bean。 But if there was a couple of methods, that would return MyCustomComponent, then you would have to tell Spring which one to inject with @Qualifier annotation.但是如果有几个方法,它们会返回 MyCustomComponent,那么您将必须告诉 Spring 要用 @Qualifier 注释注入哪个。
SIDENOTE: @Bean annotation by default Spring uses the method name as a bean name.旁注:@Bean 注解默认 Spring 使用方法名作为 bean 名。 You can also assign other name like @Bean("otherComponent").您还可以分配其他名称,如@Bean("otherComponent")。
This is you interface:这是你的界面:
public interface TestRepository{}
This is your implementation 1:这是您的实施 1:
@Repository
public class Test1Repository implements TestRepository{}
Your implementation 2:您的实施 2:
@Repository
public class Test2Repository implements TestRepository{}
Now you are injecting it like:现在你正在注入它:
private final TestRepository testRepository;
public TestServiceImpl(TestRepository testRepository) {
this.testRepository= testRepository;
}
QUESTION?题? How is Spring supposed to know which class to inject? Spring 应该如何知道要注入哪个 class? Test1 or Test2.测试 1 或测试 2。 That's why you tell it with @Qualifier which class.这就是为什么你用 @Qualifier 告诉它哪个类。
private final TestRepository testRepository;
public TestServiceImpl(@Qualifier("test1Repository") TestRepository testRepository) {
this.testRepository= testRepository;
}
I Prefer different method to not
using @Qualifier我更喜欢not
使用 @Qualifier 的不同方法
public interface CommonFooBar{ public String commonFoo(); public String commonBar(); }
public interface FooService extends CommonFooBar { } public interface BarService extends CommonFooBar { }
@Autowired FooService fooService; or @Autowired BarService barService;
so, we can defined the single responsibility to each interface and This kind of segregation is more readable to every junior.所以,我们可以为每个接口定义单一的职责,这种分离对每个初级人员来说更具可读性。
I quite like a different way of working.我非常喜欢不同的工作方式。 Surely if you provide a unique name for your bean, then that is all you need?当然,如果您为您的 bean 提供了一个唯一的名称,那么这就是您所需要的了吗?
Given the example below, its easy to see that Spring will name the beans based on the method name used to create the beans.在下面的示例中,很容易看出 Spring 将根据用于创建 bean 的方法名称来命名 bean。 In other words, if you give your beans sensible names, then the code should become self-explanatory.换句话说,如果您给 bean 起合理的名称,那么代码应该是不言自明的。 This also works when injecting beans into other classes.这在将 bean 注入其他类时也有效。
The end result of this is:这样做的最终结果是:
Lets not over-complicate Spring.让我们不要过于复杂 Spring。
@Bean
mqConnectionFactory() {
ConnectionFactory connectionFactory = new MQXAConnectionFactory();
return connectionFactory;
}
@Bean
public ConnectionFactory pooledConnectionFactory(ConnectionFactory mqconnectionFactory) {
JmsPoolConnectionFactory connectionFactory = new JmsPoolConnectionFactory();
connectionFactory.setConnectionFactory(mqConnectionFactory);
return connectionFactory;
}
@Bean
public ConnectionFactory cachingConnectionFactory(ConnectionFactory mqConnectionFactory) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setTargetConnectionFactory(mqConnectionFactory);
return connectionFactory;
}
@Bean
public JmsTemplate jmsTemplate(ConnectionFactory cachingConnectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
return jmsTemplate;
}
@Bean
public DefaultMessageListenerContainer messageListenerContainer(ConnectionFactory pooledConnectionFactory) {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(pooledConnectionFactory);
...
return container;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.