-2

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....

naimul64
  • 133
  • 3
  • 17
Abrar Ansari
  • 127
  • 8
  • 4
    Where is your CreateUser bean creation. You have to create it in your Configuartion class. If their is no bean hor Sping IOC controller will Autowire the CreateUser bean in your User class. – SachinSarawgi Sep 12 '17 at 06:56
  • Neither of these codes makes any sense. It's like someone just copied code randomly, not understanding what he's doing. – Kayaman Sep 12 '17 at 06:58
  • In default constructor case where Autowire is working because there is nothing to Autowire. Try to use Autowire above CreateUser attribute in case of default constructor it will fail there too. – SachinSarawgi Sep 12 '17 at 06:59
  • i am doing autowiring for bean creation...i dont have to much knowledge as i am new in spring. – Abrar Ansari Sep 12 '17 at 06:59
  • Then it's time to read the documentation. – Kayaman Sep 12 '17 at 06:59
  • Do you have CreateUser class, could you please post it here. – SachinSarawgi Sep 12 '17 at 07:00

1 Answers1

1

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.

SachinSarawgi
  • 2,632
  • 20
  • 28
  • createUser is an interface not a class here it is...package profile; public interface CreateUser { void addUser(String name, String contact); void showUser(); } – Abrar Ansari Sep 12 '17 at 07:04
  • If its an interface then there must be an implementation of it. Do you have any implementation of it?? If yes then use @Bean annotation above that implementation class, it will work. – SachinSarawgi Sep 12 '17 at 07:04
  • What is `CreateUser` supposed to be? Why would `User` need a `CreateUser`? – Kayaman Sep 12 '17 at 07:06
  • no....but if i implement then the code runs perfectly...can someone please explains???? – Abrar Ansari Sep 12 '17 at 07:07
  • Yes, it will run perfectly if you have implementation. If you autowire CreateUser which is an interface and if there is no implementation of it then how Spring IOC controller will autowire. Because it will need concrete implementation of it.. – SachinSarawgi Sep 12 '17 at 07:09