NoSuchBeanDefinitionException: no qualification bean of type 'int'

I am a complete newbie to Spring.

I'm trying to check if I can check RequestMapping, RequestBody, RequestResponse and RestTemplate.

I wanted to get this object as an answer:

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 he 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 I've done?

// EDIT

Thanks to KrishnaKuntala, this was simply because I did not have a default constructor.

The statement of the question is immediately eliminated.

+5
source share
3 answers

You can enter simple properties and you can easily access properties using the @Lalue 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 applicationContext:

 <context:property-placeholder .../> 

Note

If you fix it with the default constructor, you will need a different mechanism to initialize your bean, so you need to know what you are doing if you want to add a non arg constructor instead of the previous one.

+3
source

You do not need to use the default argument constructor no to create a bean. In your case:

1) If you use an XML configuration and want to use a constructor that takes arguments, you need to specify them with the constructor-arg element as follows:

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

2) If you are using the 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 useful article on constructor injection in spring .

+2
source

Try adding a default constructor (without a parameter constructor) to 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; } } 
0
source

Source: https://habr.com/ru/post/1268416/


All Articles