【问题标题】:UnsatisfiedDependencyException of JpaRepositoriesJpaRepositories 的 UnsatisfiedDependencyException
【发布时间】:2019-07-07 11:47:27
【问题描述】:

我正在编写一个 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> {

}

我得到的异常:

线程“main”中的异常 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 吗?

【问题讨论】:

  • 您将不兼容的版本混合在一起。例如,一些 spring deps 的版本是 LATEST,其他的有 5.1.7.RELEASE,其他的有 5.1.5.RELEASE。不。并且不要使用过时的 hibernate-jpa-2.0-api,它是 javax.persistence-api 的旧版本
  • @JBNizet 我用所有 spring 依赖项的最新版本更正了我的 pom 文件,我删除了 hibertnate 依赖项。但问题是一样的。
  • 我知道我可以使用 spring boot 来简化依赖项管理,但出于学习目的,我自己编写依赖项。
  • 类 SynchronizationType 是 javax.persistence-api 依赖项的一部分。所以如果你的运行时类路径中确实有那个 jar 文件,我看不出你怎么会有这个异常。
  • 确切地说,当我更正我的 pom.xml 文件时,我得到 java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;

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


【解决方案1】:

缺少一个数据源和一个 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;
    }

【讨论】:

    【解决方案2】:

    你可以试试下面的版本,SynchronizationType 从 2.1 开始提供。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2020-10-19
      • 1970-01-01
      • 2018-04-20
      • 2013-11-26
      • 2018-03-22
      • 1970-01-01
      相关资源
      最近更新 更多