JpaRepositories的UnsatisfiedDependencyException

时间:2019-07-07 11:47:27

标签: java spring spring-data-jpa spring-repositories

我正在编写一个jpa重新发布示例,并且遇到类型为UnsatisfiedDependencyException的运行时异常。

这是我的程序:

@Configuration
@EnableJpaRepositories(basePackageClasses = { PersonRepository.class, ProfessionRepository.class})
@ComponentScan( basePackageClasses =MyService.class)
public class SpringDataJpa  {
        public static void main( String[] args ) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringDataJpa.class);
        service myService = applicationContext.getBean(MyService.class);
    }
}

服务界面:

public interface service {
    void add( Person person );
    List<Person> getListOfPersons();
}

引发异常的实现:

@Service
public class MyService implements service {

    @Autowired
    PersonRepository personRepository;

    @Override
    public void add( Person person ){
        System.out.println("saved");
    }
    @Override
    public List<Person> getListOfPersons() {
        return  null;
    }
}

存储库:

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {

}
@Repository
public interface ProfessionRepository extends JpaRepository<Profession, Integer> {

}

我得到的例外情况:

  

线程“主”中的异常   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ myService”的bean时出错:不满意的依赖关系   通过“ personRepository”字段表示;嵌套异常为   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ personRepository”的bean:合并后处理   Bean定义失败;嵌套异常为   java.lang.NoClassDefFoundError:javax / persistence / SynchronizationType

在我检查此discussion时,我添加了建议的依赖项。我对pom.xml文件的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>LATEST</version>
    </dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>LATEST</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>LATEST</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.197</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

如何解决此错误?

我的第二个问题是:如果使用Spring Boot,应该使用@EnableJpaRepositories吗?

2 个答案:

答案 0 :(得分:0)

缺少数据源和entityManagerFactory bean。 为了解决我的问题,我在配置类中添加了以下代码:

    @Bean
    public DataSource firstDataSource (){
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/ride_tracker?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("password");
        return driverManagerDataSource;
    }

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("data");
        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        entityManagerFactoryBean.setJpaProperties(jpaProperties);
        return entityManagerFactoryBean;
    }

答案 1 :(得分:0)

您可以尝试使用以下版本,从2.1开始提供SynchronizationType。

<dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
</dependency>