这是我第一次使用Spring MVC尝试Spring Data JPA,但我无法解决此错误。
错误记录
SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'mainController'的bean时出错:通过字段'userRepository'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'com.example.repo.UserRepository'类型的限定bean可用:预期至少有1个bean可以作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) 在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4725) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189) 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1419) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1409) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745) 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'com.example.repo.UserRepository'类型的限定bean可用:预期至少有1个bean可以作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 在org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ......还有24个
UserRepository.java
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
MainController.java
@Controller
@RequestMapping("/")
public class MainController {
@Autowired
private UserRepository userRepository;
@RequestMapping(value = "/user", method = RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<User> addUser(@RequestBody User user) {
userRepository.save(user);
System.out.println("------>>>>>>>>>>" + user.getName());
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<User>(headers, HttpStatus.OK);
}
@RequestMapping(value = "/user/list", method = RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
List<User> users = (List<User>)userRepository.findAll();
if (users.isEmpty()) {
System.out.println("List is empty oops..!!");
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
}
System.out.println("Got the List...!!");
System.out.println("Name of First User---->>>>" + users.get(0).getName());
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
}
AppConfig.java
@Configuration
@ComponentScan("com.example")
@EnableWebMvc
@EnableJpaRepositories
@EnableTransactionManagement
public class AppConfig {
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF");
resolver.setSuffix(".html");
return resolver;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring_database");
dataSource.setUsername("root");
dataSource.setPassword("");
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory(){
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(User.class.getPackage().getName());
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
User.java
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String email;
/*Getters and Setters*/
这是我的项目结构
我在这里缺少什么?或者我在这里犯了什么错误?任何人都可以解决这个问题吗?
答案 0 :(得分:0)
您需要告诉spring在哪里寻找存储库:
@EnableJpaRepositories(basePackageClasses = UserRepository.class)