简体   繁体   English

NoSuchBeanDefinitionException: 没有“int”类型的合格 bean

[英]NoSuchBeanDefinitionException: No qualifying bean of type 'int'

I am a complete beginner in Spring.我是 Spring 的完全初学者。

I am right now trying to see if I can test RequestMapping, RequestBody, RequestResponse and RestTemplate.我现在正在尝试查看是否可以测试 RequestMapping、RequestBody、RequestResponse 和 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'?但是我如何真正为'int'创建一个bean定义?

What have I done?我做了什么?

// EDIT // 编辑

Thanks to KrishnaKuntala, it was just because i did not have a default constructor.感谢 KrishnaKuntala,这只是因为我没有默认构造函数。

Putting one immediately resolved the issue.放置一个立即解决了问题。

You can Injecting simple properties and can easily access the properties with @Value annotation and placeholders:您可以注入简单的属性,并且可以使用 @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;
    }
}

Then you need to add them to the applicationContext:然后你需要将它们添加到 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.如果您使用默认构造函数修复它,您将需要另一种机制来初始化您的 bean,因此,如果您想添加非 arg 构造函数而不是执行之前的构造,您必须知道自己在做什么。

You are not required to only use a default no arguments constructor to create a bean.您不需要只使用默认的无参数构造函数来创建 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: 1) 如果您使用 XML 配置并希望使用接受参数的构造函数,则需要使用 constructor-arg 元素指定它们,如下所示:

<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: 2) 如果您使用 Java 配置类,您将需要这样的东西:

@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 构造函数注入的有用文章。

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

Please try adding a default (no parameter constructor) in your TestObjecImpl class.请尝试在您的 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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoSuchBeanDefinitionException:没有类型合格的bean(存储库) - NoSuchBeanDefinitionException: No qualifying bean of type (repository) NoSuchBeanDefinitionException:找不到类型合格的bean - NoSuchBeanDefinitionException: No qualifying bean of type found NoSuchBeanDefinitionException: 没有“XInterceptor”类型的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type "XInterceptor" Spring Data Redis NoSuchBeanDefinitionException:没有类型的限定bean - Spring Data Redis NoSuchBeanDefinitionException: No qualifying bean of type NoSuchBeanDefinitionException:没有内部类的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type for inner class NoSuchBeanDefinitionException:未找到依赖关系类型为JpaVendorAdapter的合格Bean - NoSuchBeanDefinitionException: No qualifying bean of type JpaVendorAdapter found for dependency NoSuchBeanDefinitionException:没有要自动装配的JobLauncher类型的合格Bean - NoSuchBeanDefinitionException: No qualifying bean of type JobLauncher to autowire NoSuchBeanDefinitionException:没有符合条件的 bean 类型(JpaRepository 和 Java Config) - NoSuchBeanDefinitionException: No qualifying bean of type (JpaRepository and Java Config) 如何解决“ NoSuchBeanDefinitionException:没有类型的合格bean”错误? - How to cure 'NoSuchBeanDefinitionException: No qualifying bean of type' error? NoSuchBeanDefinitionException: 没有合格的 bean - NoSuchBeanDefinitionException: No qualifying bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM