@Autowired any method name with any number of arguments

I am trying to annotate a dialing method as follows:

package com.spring.examples; public class MyBean { private String name; private int age; @Autowired public void set(String name, int age) { this.name = name; this.age = age; } } 

configuration file:

 <bean id="myBean" class="com.spring.examples.MyBean"> <property name="name" value="Marie" /> <property name="age" value="101" /> </bean> 

I got this error:

Qualified bean type [java.lang.String] found for dependency not found: at least 1 bean is expected that qualifies

How can I configure this bean to properly call the set method?

+4
source share
2 answers

It's good to use @Autowired for a method with any number of arguments. The only problem is that the application context should be able to determine what you want to enter for each of these arguments.

The complaint in the error message makes this very clear: you do not have a unique String bean defined in the context of your application.

The solution for your specific example would be to use the @Value annotation for each of the arguments:

 @Autowired set(@Value("${user.name:anonymous}") String name, @Value("${user.age:30}") int age) 

This will allow you to use the PropertyPlaceholderConfigurer defined in your context to resolve these properties and revert to the provided default values ​​if these properties are not defined.

If you want to introduce objects that are defined as beans in your context, you only need to make sure that for each argument you need only one bean match:

 @Autowired set(SomeUniqueService myService, @Qualifier("aParticularBean") SomeBean someBean) 

The above example assumes that in the application context there is only one instance of SomeUniqueService , but there can be several instances of SomeBean , however only one of them will have a bean id "aParticularBean".

As a final note, this use case for @Autowired most suitable for constructors, as it rarely happens when you need to set properties as massive once an object has been created.

Edit:

I noticed your XML configuration after writing the answer; it is completely useless. If you want to use annotations, just define a bean without any properties and make sure you declare <context:annotation-config/> somewhere in your context:

 <context:annotation-config/> <bean id="myBean" class="com.spring.examples.MyBean"/> <!-- no properties needed since the annotations will be automatically detected and acted upon --> 

Thus, the container will detect everything that needs to be entered and act accordingly. The XML element <property/> can only be used to call java bean sets (which take only one argument).

Alternatively, you can annotate your class with a stereotype like @Component (or @Service or something else), and then just use <context:component-scan/> ; this eliminates the need to declare each individual bean in XML.

+10
source

You can also define String and Integer beans as follows:

 <bean id="name" class="java.lang.String"> <constructor-arg value="Marie"/> </bean> <bean id="age" class="java.lang.Integer"> <constructor-arg value="101"/> </bean> 

But I think this is strange for such a simple case. Instead, I would go for two setters, as suggested in the comments:

 package com.spring.examples; public class MyBean { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } } 
0
source

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


All Articles