@Repository接口的自动装配在具体类中失败

时间:2015-03-02 02:20:57

标签: spring spring-mvc

我有一个实现接口的具体类,如下所示:

@Service
public class BaseImpl implements Base{
  @Autowired
  Details details;
  ...
  public void doSomething(){
  }
}

界面代码:

@Service
public interface Base{
  void doSomething();
}

我的配置类:

@Configuration
public class AppConfig {
    @Bean(name="samplebean")
    public Base getImpl(){
        return new BaseImpl();
    }
}

详细信息界面(使用@Repository注释):

@Repository
public interface Details extends CrudRepository<..., ...>{
    ...
}

最后,我调用BaseImpl类如下:

@RestController
@RequestMapping(value = "...")
public class Caller{
  public void foo(){
    Base b = (Base) context.getBean("samplebean");
    b.doSomething();
  }
}

我收到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.model.Details com.pkg.BaseImpl.details; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.model.Details] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 35 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.model.Details] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 37 more

然而,当我在Caller类中进行相同的自动连接时(或者对于任何其他未实现接口的类),它似乎工作正常。我是Spring的新手,所以我可能会在这里遗漏一些东西。

感谢。

2 个答案:

答案 0 :(得分:1)

你需要告诉spring有关Details bean的信息。将它声明为bean,

@Configuration
public class AppConfig {
    @Bean(name="samplebean")
    public Base getImpl(){
        return new BaseImpl();
    }

    @Bean
    public Details details() {
        return new Details();
    }
}   

或使用@Component(或其他更合适的刻板印象)注释

@Component
public class Details {
    // ...
}

答案 1 :(得分:1)

由于您使用Details注释了@Repository类,因此您只需要使用正确的基本包名称将@ComponentScan注释添加到配置类中。

@ComponentScan(basePackages = { "<yourBasePackage>" })