0

I have following sample code:

Bean class:

package com.example.learn1.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Bean1 {

    private String name;
    private int id;

    public Bean1() {
        System.out.println("Inside Bean1() constructor");
    }

    @Autowired
    public Bean1(String name, int id) {
        System.out.println("Inside Bean1(String name, int id) constructor");
        this.name = name;
        this.id = id;
    }

    public Bean1(String name) {
        System.out.println("Inside Bean1(String name) constructor");
        this.name = name;
    }

    public Bean1(int id) {
        System.out.println("Inside Bean1(int id) constructor");
        this.id = id;
    }

}

The below is main app, here I am trying to autowire bean1.

package com.example.learn1.main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.example.learn1.bean.Bean1;
@Configuration
@ComponentScan(basePackages = { "com.example.learn1" })
@Component
public class MainApp {

    @Autowired
    Bean1 bean1;
    
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(MainApp.class);
        System.out.println("Inside main, ctx is --> " + ctx);
        MainApp t = ctx.getBean(MainApp.class);
        t.print();
    }

    public void print() {
        System.out.println("Inside MainApp#print() method");
    }
}

The program works fine when I don't keep @Autowire on constructor public Bean1(String name, int id). By default it calls the no-argument constructor viz: public Bean1(). The moment I use @Autowire on constructor public Bean1(String name, int id), it fails.

So does it work only on no-argument Constructor? How can I make it work for Constructor with arguments?

The below is the exception:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainApp': Unsatisfied dependency expressed through field 'bean1'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bean1' defined in file [/Users/vkoul/eclipse-workspace/spring-framework-learning/target/classes/com/example/learn1/bean/Bean1.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
    at com.example.learn1.main.MainApp.main(MainApp.java:37)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bean1' defined in file [/Users/vkoul/eclipse-workspace/spring-framework-learning/target/classes/com/example/learn1/bean/Bean1.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1244)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 14 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1651)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
    ... 27 more
halfer
  • 19,824
  • 17
  • 99
  • 186
CuriousMind
  • 8,301
  • 22
  • 65
  • 134

1 Answers1

2
@Autowired
public Bean1(String name, int id) {
    System.out.println("Inside Bean1(String name, int id) constructor");
    this.name = name;
    this.id = id;
}

Tells to the container to inject 2 beans (one bean of type String called name and the other one called id of type int) when the constructor is used to instantiate bean1. Where did you defined those beans? Also note that creating beans from primitive types is not common, that does not mean you can't.

For primitive type we generally declared them in application.properties and then inject them using @Value.

akuma8
  • 4,160
  • 5
  • 46
  • 82
  • For simplicity i kept primitives, I can very well keep some Objects. My question is more inclined towards how to inject using a constructor which has arguments. – CuriousMind Feb 07 '20 at 14:49
  • 1
    As I said in my answer `Where did you define those beans?`. If you want to inject beans as constructor parameters, you have to define them first. So define 2 beans of type `String` and `int` then they will be picked and injected into your constructor. – akuma8 Feb 07 '20 at 14:54
  • @akumar8: Thanks for your inputs. So if we do `@Autowired` on a `field`, why do we still need to define a `constructor` or a `setter method`? – CuriousMind Feb 07 '20 at 15:42
  • 1
    @CuriousMind You do not need `setter` neither `constructor` when using `@Autowired` on a field. You have 3 ways to inject beans, `field injection` with `@Autowired`, `setter` injection and `constructor` injection, it's up to you to choose one. – akuma8 Feb 07 '20 at 15:55
  • That is my doubt, when we use `field injection` why it still calls `constructor`. – CuriousMind Feb 07 '20 at 15:57
  • 1
    When using `field injection`, behind the scene the container constructs your object using the default constructor and then set the fields values. – akuma8 Feb 07 '20 at 16:05