Reputation: 3248
I have the following classes and interfaces in my spring webapp and I am trying to autowire them. Spring complains with the following exception:
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 class
@RestController
@RequestMapping("/employee")
public class EmployeeController{
@Autowired
EmployeeService employeeService;
}
EmployeeService Interface
public interface EmployeeService {
}
EmployeeServiceImpl class
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeRepository employeeRepository;
}
EmployeeRepository interface
@Repository
public interface EmployeeRepository extends EmployeeRepositoryCustom, JpaRepository<Employee, String>, JpaSpecificationExecutor<Employee>{
}
EmployeeRepositoryCustom interface
@Repository
public interface EmployeeRepositoryCustom {
}
EmployeeRepositoryImpl class
@Component
public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom{
@PersistenceContext
EntityManager em;
}
The config classes look like below:
@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 class
@Configuration
@Import(value = {DataSourceConfig.class, WebMvcConfig.class, RepositoryConfig.class})
@ComponentScan(basePackages = "com.xyz",
[email protected](value = Configuration.class, type= FilterType.ANNOTATION))
@EnableSpringConfigured
public class SpringConfig {
@Autowired
Environment env;
}
I'm not able to figure out what's wrong in autowiring. Any help would be appreciated. Thanks.
Upvotes: 1
Views: 1274
Reputation: 124526
@Repository
public interface EmployeeRepository extends EmployeeRepositoryCustom, JpaRepository<Employee, String>, JpaSpecificationExecutor<Employee>{
}
Judging from the above repository definition I suspect you are trying to use [Spring Data JPA]. However only extending a Spring Data interface isn't enough you also need to enable it with the @EnableJpaRepository
annotation. Put that on your @Configuration
class.
@Configuration
@EnableWebMvc
@EnableJpaRepositories("your-base-package-here")
@ComponentScan(basePackages = { "com.xyz" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {
}
Upvotes: 1