弹簧接线时接口异常不满意

时间:2017-04-19 11:02:10

标签: java spring autowired

我的spring webapp中有以下类和接口,我正在尝试自动装配它们。 Spring抱怨以下例外:

Error creating bean with name 'employeeServiceImpl': Unsatisfied 
dependency expressed through field 'employeeRepository'; nested exception 
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type 'com.xyz.repository.EmployeeRepository' 
available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

EmployeeController类

@RestController
@RequestMapping("/employee")
public class EmployeeController{
    @Autowired
    EmployeeService employeeService;
}

EmployeeService接口

public interface EmployeeService {

}

EmployeeServiceImpl类

@Service
public class EmployeeServiceImpl implements EmployeeService{
    @Autowired
    EmployeeRepository employeeRepository;  
}

EmployeeRepository接口

@Repository
public interface EmployeeRepository extends EmployeeRepositoryCustom, JpaRepository<Employee, String>, JpaSpecificationExecutor<Employee>{

}

EmployeeRepositoryCustom接口

@Repository
public interface EmployeeRepositoryCustom {

}

EmployeeRepositoryImpl类

@Component
public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom{
    @PersistenceContext
    EntityManager em;

}

配置类如下所示:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.xyz" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {

}

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {WebMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected void registerDispatcherServlet(ServletContext servletContext) {
        super.registerDispatcherServlet(servletContext);

        servletContext.addListener(new HttpSessionEventPublisher());
    }

}

SpringConfig类

    @Configuration
    @Import(value = {DataSourceConfig.class, WebMvcConfig.class, RepositoryConfig.class})
    @ComponentScan(basePackages = "com.xyz", 
        excludeFilters=@ComponentScan.Filter(value = Configuration.class, type= FilterType.ANNOTATION))
    @EnableSpringConfigured
    public class SpringConfig {
        @Autowired
        Environment env;

    }

我无法弄清楚自动装配有什么问题。任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

@Repository
public interface EmployeeRepository extends EmployeeRepositoryCustom, JpaRepository<Employee, String>, JpaSpecificationExecutor<Employee>{

}

从上面的存储库定义判断我怀疑你正在尝试使用[Spring Data JPA]。但是,仅扩展Spring Data接口是不够的,您还需要使用@EnableJpaRepository注释启用它。把它放在@Configuration班上。

@Configuration
@EnableWebMvc
@EnableJpaRepositories("your-base-package-here")
@ComponentScan(basePackages = { "com.xyz" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {

}