-2

This is the error message I got

Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}] with root cause org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

UPDATE:
This is the class that I'm trying to autowire, Actually it is the service class with a parameterized constructor.

@Component
@Scope("prototype")
public class RelatedProductService {

    private String aa;

    private String bb;

    private String cc;


    @Autowired
    public RelatedProductService(String aa, String aa, String orgId, String cc) {
        super();
        this.aa = aa;
        this.bb = bb;
        this.cc = cc;
    }

...
}

And this is where I want to use it, The controller class.

@Controller("relatedProductsV1")
@RequestMapping("api/v1/related-products")
@Scope("prototype")
public class RelatedProductsController extends BaseController {

    @Autowired
    private RelatedProductService relatedProductService;

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String getRelatedProducts( @RequestParam(value = "aa", required = true) String aa,
        @RequestParam(value = "bb", required = true) String bb,
        @RequestParam(value = "cc", required = true) String cc) {
 ...


        String response = relatedProductService.getRelatedProductsResponse();
...
    }
}

Hope this helps.

  • 1
    It says: The `String` you're referencing is not registered as a `Bean`? What don't you understand about this message? – Lino Mar 04 '19 at 12:06
  • Possible duplicate of [Is there a way to @Autowire a bean that requires constructor arguments?](https://stackoverflow.com/questions/6739566/is-there-a-way-to-autowire-a-bean-that-requires-constructor-arguments) – Draken Mar 04 '19 at 12:58
  • Can you tell us what exactly you want the code to do? For us it's quite an obvious error, because you're trying to autowire strings (`aa`, `bb`, `cc`) and there are no such beans of the type `String`. I'm seeing a controller with similar parameters, but I fail to see how those two are connected (or should be connected). – g00glen00b Mar 19 '19 at 19:25

3 Answers3

0

Without much context, it would look like you're trying to autowire a bean which has at least a non-default constructor (one that takes String as a parameter). You should either:

  • Add a default constructor to the class
  • Add a parameter to the bean declaration (That would depend on whether you're using XML or Java classes based definitions, but the principle remains the same)
Waddah Ajaj
  • 121
  • 1
  • 10
  • Waddah Ajaj just updated the question. Please check. – Gayan Bandara Mar 04 '19 at 12:31
  • That's exactly the problem: you have a non default constructor, and you're trying to autowire without providing arguments. You should add a bean declaration to the configuration file (I assume you have java-based configuration here). Something like: @Bean public RelatedProductService relatedProductService(){ return new RelatedProductServiceImpl("arg1", "arg2", "arg3"); } – Waddah Ajaj Mar 04 '19 at 13:23
0

remove the annotations from the first class and @Autowired private RelatedProductService relatedProductService; from the second class and it should work

With this construct you are not auto-wiring anything because you're initiating the "injected" service yourself.

mahieus
  • 580
  • 3
  • 17
0

First of all thanks everybody for your valuable thoughts/ suggestions, Highly appreciate them all.

I think I have made a mistake in the first place going for a constructor to inject the properties/constructor agruements coming in as user request params, which indeed will not be in place when the application context loads up. The ideal usage of a constructor of an autowired component would be when the properties/constructor arguments are constants which will be residing in a properties file.

So I chose to go for setters and solved my issue. I must say that, all your answers and assistance helped me to reach this path.