Issue
I am doing a simple test on autowiring in spring bean but autowiring do not run if interface is passed as my constructor argument. Here is the code for bean
package profile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class User
{
private CreateUser user;
@Autowired
public User(CreateUser user)
{
this.user = user;
}
public void addUser(String name, String contact)
{
System.out.println("Adding "+name+" and "+contact+".....");
}
public void showUser()
{
System.out.println("Added Successfully");
}
}
My automatic config class
package profile;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class UserConfig {
}
and my main class
package profile;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class UserMain {
public static void main(String ar[])
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
User users = context.getBean(User.class);
users.addUser("test", "122466");
users.showUser();
context.close();
}
}
I am getting the following error:
run:
Sep 12, 2017 12:20:28 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1ed6993a: startup date [Tue Sep 12 12:20:28 IST 2017]; root of context hierarchy
Sep 12, 2017 12:20:28 PM org.springframework.context.annotation.AnnotationConfigApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'user' defined in file [D:\JsonWithJava\build\classes\profile\User.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'profile.CreateUser' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'user' defined in file [D:\JsonWithJava\build\classes\profile\User.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'profile.CreateUser' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at profile.UserMain.main(UserMain.java:9)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'profile.CreateUser' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 14 more
C:\Users\Admin\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
But if I use default constructor the program works fine like this:
package profile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class User
{
private CreateUser user;
@Autowired
public User()
{
}
public void addUser(String name, String contact)
{
System.out.println("Adding "+name+" and "+contact+".....");
}
public void showUser()
{
System.out.println("Added Successfully");
}
}
The code runs perfectly. Here is the output:
run:
Sep 12, 2017 12:21:49 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1ed6993a: startup date [Tue Sep 12 12:21:49 IST 2017]; root of context hierarchy
Adding test and 122466.....
Added Successfully
Sep 12, 2017 12:21:49 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1ed6993a: startup date [Tue Sep 12 12:21:49 IST 2017]; root of context hierarchy
BUILD SUCCESSFUL (total time: 0 seconds)
please help....
Solution
In your Configuration class write below code:
@Configuration
@ComponentScan
public class UserConfig {
//If their is default constructor in CreateUser then
@Bean
public CreateUser createUserBeanCreate(){
return new CreateUser();
}
}
Another option would be to use @Bean above your CreateUser class:
@Bean
public class CreateUser{
}
If CreatweUser is an interface (which is your case), then you need an implementation of CreateUser and use @Bean annotation above that class, so that Spirng IOC container can autowire CreateUser implementation whenever you use autowire above CreateUser. Hope this helps.
Answered By - SachinSarawgi