2017-05-31 53 views
5

我是一個完整的初學者在春天。NoSuchBeanDefinitionException:沒有類型'int'的合格bean

我現在試圖看看我是否可以測試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,那只是因爲我沒有默認的構造函數。

立即解決問題。

+0

您如何期待Spring實例化TestObjectImpl? –

+0

請嘗試在TestObjecImpl類中添加一個默認的參數(無參數構造函數)。 –

+0

@SotiriosDelimanolis我在另一個主類上有@SpringBootApplication。我相信如果我在'TestObjectImpl'上有@Component,Spring將自動掃描它。 –

回答

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,因此,如果您想添加非參數構造函數而不是執行前面的操作,則必須知道自己在做什麼。

+0

@SotiriosDelimanolis,是的,你是對的。如果你不介意,你能檢查我更新的答案嗎?讓我知道如果我說錯了什麼。 (並且很抱歉發生了什麼,我的不好) –

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

他們爲什麼要這樣做?爲什麼它目前失敗了,你的建議有什麼改變? –

2

您不需要僅使用默認的無參數構造函數來創建一個bean。你的情況:

1)如果你使用XML配置並希望使用一個構造函數中的參數,你需要與構造帶參數的元素指定它們像這樣:

<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

相關問題