[英]NoSuchBeanDefinitionException: No qualifying bean of type (repository)
[英]NoSuchBeanDefinitionException: No qualifying bean of type 'int'
我是 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,這只是因為我沒有默認構造函數。
放置一個立即解決了問題。
您可以注入簡單的屬性,並且可以使用 @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 構造函數而不是執行之前的構造,您必須知道自己在做什么。
您不需要只使用默認的無參數構造函數來創建 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");
}
}
看看這篇關於spring 構造函數注入的有用文章。
我正在使用 spring web-flow 並且該類沒有加載並且必須在 config servlet xml 中聲明。
請嘗試在您的 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.