I am creating a simple CRUD application using spring boot and hibernate with ajax requests from jsp page. i got working this to the controller and after when i extend my Repo class with JpaRepository i am geting no beans found errors.
i have tried to enable entity manager in web config class an then i also get no beans found for entity manager.
This is my controller class
@Controller
public class EmployeeController {
@Autowired
private EmployeeRepo employeeRepo;
@GetMapping("/")
public String employeeForm() {
return "employeeForm";
}
@PostMapping(value = "/saveEmployee", produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public boolean saveEmployee(@ModelAttribute @Valid Employee employee, BindingResult result) {
Employee save = employeeRepo.save(employee);
return true;
}
}
this is my custom repository
public interface EmployeeRepo extends JpaRepository<Employee,Integer> {
}
this is my web config class
@配置 @EnableWebMvc
@ComponentScan(basePackages = { "com.boraji.tutorial.spring.controller" })
@ComponentScan(basePackages = { "com.boraji.tutorial.spring.repo" })
@EnableJpaRepositories(basePackages = "com.boraji.tutorial.spring.repo", entityManagerFactoryRef = "entityManagerFactory")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator(){
return new HibernateExceptionTranslator();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
this is my webapp initializer class
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
这是错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepo': Cannot create inner bean '(inner bean)#7dc21c89' of type
[org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7dc21c89': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available