NoSuchBeanDefinitionException:没有类型' int'的限定bean

时间:2017-05-31 23:07:48

标签: java spring

我是春天的初学者。

我现在正试图看看我是否可以测试RequestMapping,RequestBody,RequestResponse和RestTemplate。

我希望收到此对象作为回复:

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;
    }
}

但是,我得到了:

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]

所以,它说,

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.

但是我如何真正为' int'?

创建bean定义

我做了什么?

//编辑

感谢KrishnaKuntala,这只是因为我没有默认的构造函数。

立即解决问题。

4 个答案:

答案 0 :(得分:3)

您可以注入简单属性,并可以使用@Value注释和占位符轻松访问属性:

@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;
    }
}

然后你需要将它们添加到applicationContext:

<context:property-placeholder .../>
  

请注意

如果使用默认构造函数修复它,则需要另一种机制来初始化bean,因此,如果要添加非arg构造函数而不是执行上一个构造函数,则必须知道自己在做什么。

答案 1 :(得分:2)

您不需要仅使用默认的无参数构造函数来创建bean。在你的情况下:

1)如果您正在使用XML配置并且想要使用接受参数的构造函数,则需要使用constructor-arg元素指定它们,如下所示:

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

2)如果你使用Java配置类,你需要这样的东西:

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

请查看有关constructor injection in spring的有用文章。

答案 2 :(得分:0)

请尝试在TestObjecImpl类中添加默认值(无参数构造函数)。

@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;
    }
}

答案 3 :(得分:0)

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