UnsatisfiedDependencyException

UnsatisfiedDependencyException

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

最佳答案

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

关于java - JpaRepositories的UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56921885/

10-10 16:19