Is it possible to use multiple @Qualifier annotations in Spring?

I have a beans set that has two properties. They are mainly serializers for different classes and for different purposes.

For example, there might be a serializer Orderfor a local log, a Orderserializer for registering a webservice call, a serializer Customerfor tracking URLs, and a Customerserializer for tracking URLs.

This is why I would like to use two annotations @Qualifier:

@Autowired
@Qualifier("order")
@Qualifier("url")
private Serializer<Order> orderSerializer;

Unfortunately, the compiler complains about duplicate annotations in this case. Are there any workarounds or alternative solutions to this problem?

+3
source share
3 answers
@Qualifier("order-url")

order-url

@Component("order-url")
+4

, , , Spring 2.5.

, @Qualifier, . Spring , .

, :

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MyOrderQualifier {
}

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MyUrlQualifier {
}

, .

@Autowired
@MyOrderQualifier
@MyUrlQualifier
private Serializer<Order> orderSerializer;

Spring 2.5 , . , Spring 2.5 Spring.

+9

Of course, I don’t know all the details, but this problem is more like a Decorator pattern task . Perhaps you can link this in a Spring configuration if necessary.

Or, I agree with Bojo here, you could use some conventions about expressing Spring beans through you so that the name bean can reflect its responsibility and scope.

0
source

Source: https://habr.com/ru/post/1760296/


All Articles