在进行自动装配时Bean创建失败

时间:2017-09-12 06:55:37

标签: java spring spring-bean

我正在对spring bean中的自动装配进行一个简单的测试,但是如果接口作为我的构造函数参数传递,则无法运行自动装配。这是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");
    }
}

我的自动配置类

package profile;


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class UserConfig {


}

和我的主要班级

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();
    }
}

我收到以下错误:

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)

但是如果我使用默认构造函数,程序就可以正常工作:

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");
    }
}

代码运行完美。这是输出:

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)

请帮忙......

1 个答案:

答案 0 :(得分:1)

在Configuration类中编写以下代码:

@Configuration
@ComponentScan
public class UserConfig {

  //If their is default constructor in CreateUser then
  @Bean
  public CreateUser createUserBeanCreate(){
    return new CreateUser();
  }
}

另一个选择是在CreateUser类上面使用@Bean:

@Bean
public class CreateUser{

}

如果CreatweUser是一个接口(这是你的情况),那么你需要一个CreateUser的实现并在该类上面使用@Bean注释,这样只要你在CreateUser上面使用autowire,Spirng IOC容器就可以自动装配CreateUser实现。 希望这会有所帮助。