简体   繁体   中英

NoSuchBeanDefinitionException: No qualifying bean of type 'int'

I am a complete beginner in Spring.

I am right now trying to see if I can test RequestMapping, RequestBody, RequestResponse and RestTemplate.

I wanted to receive this object as response:

public interface TestObject {
    public int getId();
    public String getValue();
}

@Component
public class TestObjectImpl {

    private int id;
    private String value;

    public TestObjectImpl(int id, String value){
        this.id = id;
        this.value = value;
    }

    public int getId(){
        return id;
    }

    public String getValue(){
        return value;
    }
}

However, I get:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'int' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at io.yclub.castr.ads_java.ApplicationServer.main(ApplicationServer.java:12) [main/:na]

So, it says,

Description:
Parameter 0 of constructor in io.yclub.castr.ads_java.google.adwords.model.TestObjectImpl required a bean of type 'int' that could not be found.

Action:
Consider defining a bean of type 'int' in your configuration.

But how do I really create a bean definition for 'int'?

What have I done?

// EDIT

Thanks to KrishnaKuntala, it was just because i did not have a default constructor.

Putting one immediately resolved the issue.

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 .

我正在使用 spring web-flow 并且该类没有加载并且必须在 config servlet xml 中声明。

Please try adding a default (no parameter constructor) in your TestObjecImpl class.

@Component
public class TestObjectImpl {

    private int id;
    private String value;

    public TestObjectImpl(){
    }

    public TestObjectImpl(int id, String value){
        this.id = id;
        this.value = value;
    }

    public int getId(){
        return id;
    }

    public String getValue(){
        return value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM