means that in all fields of beans dependencies will be automatically entered, if not mor...">

Autowiring Priority

<beans default-autowire="byType" /> 

means that in all fields of beans dependencies will be automatically entered, if not more than 1 bean with the desired type.

I wonder if there is a way to determine some sort of priority order (for example, based on a naming convention) for automatic posting when there is more than one bean of the desired type.
Thanks in advance.

Edit: I just want to add that I'm not allowed to use annotations like @Component and @Qualifier in the project I'm working on now.

+4
source share
1 answer

No, no, but you can override this behavior as needed for each bean, for example, specify something like this when necessary:

 <beans default-autowire="byType" > <bean id="..." autowire="byName"> .... </bean> </beans> 

From spring 2.5 upwards when using <context:component-scan/> to auto-arrange beans via @Autowired you can also add @Qualifier where you must specify a bean by name if there are several beans of the same type.

As pointed out in the spring documentation , there are several ways to specify autowiring:

  • no - not autwire, this is the default value
  • byType - the type of the property must match the type of bean, if there is more than one bean of this type, then an exception is thrown
  • byName - bean name must match the property name
  • the constructor is basically the same as byType, but for constructors, spring selects the constructor with the most matches
  • autodetect is the same as byType if there is no default constructor, where it returns to constructor auto-negotiation
+2
source

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


All Articles