Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring中连接接口时出现未满足的依赖项异常_Java_Spring_Autowired - Fatal编程技术网

Java spring中连接接口时出现未满足的依赖项异常

Java spring中连接接口时出现未满足的依赖项异常,java,spring,autowired,Java,Spring,Autowired,我的SpringWebApp中有以下类和接口,我正在尝试自动连接它们。Spring抱怨,但有以下例外: Error creating bean with name 'employeeServiceImpl': Unsatisfied dependency expressed through field 'employeeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinition

我的SpringWebApp中有以下类和接口,我正在尝试自动连接它们。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;  
}
@Component
public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom{
    @PersistenceContext
    EntityManager em;

}
雇员安置界面

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

}
@Repository
public interface EmployeeRepositoryCustom {

}
EmployeeRepositoryImpl类

@Service
public class EmployeeServiceImpl implements EmployeeService{
    @Autowired
    EmployeeRepository employeeRepository;  
}
@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());
    }

}

我搞不清楚自动布线出了什么问题。任何帮助都将不胜感激。谢谢。

您使用的是弹簧还是弹簧靴?您有配置类吗?普通的spring java配置请粘贴配置类或xml配置文件的代码。您显然在使用spring数据JPA,但没有
@EnableJpaRepositories
。。。因此,在运行时不会创建存储库。您没有自动扫描正确的包。粘贴SpringConfig.java文件
@Repository
public interface EmployeeRepository extends EmployeeRepositoryCustom, JpaRepository<Employee, String>, JpaSpecificationExecutor<Employee>{

}