java - spring接线接口(interface)时出现依赖不满足异常

标签 java spring autowired

我的 spring web 应用程序中有以下类和接口(interface),我正在尝试 Autowiring 它们。 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;
}

员工服务接口(interface)

public interface EmployeeService {

}

EmployeeServiceImpl 类

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

EmployeeRepository接口(interface)

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

}

EmployeeRepository自定义接口(interface)

@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;

    }

我无法弄清楚 Autowiring 出了什么问题。任何帮助,将不胜感激。谢谢。

最佳答案

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

}

从上面的存储库定义来看,我怀疑您正在尝试使用 [Spring Data JPA]。然而,仅扩展 Spring Data 接口(interface)是不够的,您还需要使用 @EnableJpaRepository 来启用它。注解。将其放在您的 @Configuration 类中。

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

}

关于java - spring接线接口(interface)时出现依赖不满足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43493886/

相关文章:

java - 限定符在 Spring 不起作用

java - spring中的数组依赖注入(inject)?

java - Vaadin datepicker翻译月份

JAVA 文件编写器 : Get the path of created File using FileWriter

java - 如何使用 spring mvc + jackson 将嵌套的 json 对象发送到服务器

java - `NullPointerException` 由于 Autowiring 失败

java - 将 nvarchar 值 '....' 转换为数据类型 int 时转换失败

java - 无法在未调用的线程上 toast ,RxJava2

spring - spring security 3.1.2.RELEASE 中的 sessionManagementFilter

java - 找不到应用程序上下文 Spring 配置文件