NoSuchBeanDefinitionException: No qualifying bean of type 'int'

You can Injecting simple properties and can easily access the properties with @Value annotation and placeholders:

@Component
public class TestObjectImpl {
    private int id;
    private String value;

    @Autowired
    public TestObjectImpl(@Value("${prop1}")int id, @Value("${prop2}")String value){
        this.id = id;
        this.value = value;
    }

    public int getId(){
        return id;
    }

    public String getValue(){
        return value;
    }
}

Then you need to add them to the applicationContext:

<context:property-placeholder .../>

Note

If you fix it with the default constructor, you will need another mechanism to initialize your bean, so, you have to know what you are doing if you want to add the non arg constructor instead of doing the previous.


You are not required to only use a default no arguments constructor to create a bean. In your case:

1) If you're using XML configuration and want to use a constructor that takes in arguments, you need to specify them with the constructor-arg element like so:

<bean id="SomeObject" class="com.package.SomeObject">
  <constructor-arg val="someVal"/>
  <constructor-arg val="anotherVal"/>
</bean>

2) If your using a Java configuration class, you will need something like this:

@Configuration
public class Config {
    @Bean
    public SomeObject someObject() {
        return new SomeObject(1, "default");
    }
}

Have a look at this helpful article about constructor injection in spring.

Tags:

Java

Spring