Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法创建SessionFactory和接口实现bean_Java_Hibernate_Spring Boot - Fatal编程技术网

Java 无法创建SessionFactory和接口实现bean

Java 无法创建SessionFactory和接口实现bean,java,hibernate,spring-boot,Java,Hibernate,Spring Boot,当尝试运行我的应用程序时,spring由于无法创建bean而抛出一些错误消息 org.springframework.beans.factory.NoSuchBean定义异常:没有类型为“pl.coderstrust.dao.FooDao”的符合条件的bean可用:至少需要1个符合autowire候选条件的bean 创建名为“fooServiceImpl”的bean时出错:通过字段“dao”表示的未满足的依赖项 因此,我的控制器bean也无法创建: 创建名为“fooController”的be

当尝试运行我的应用程序时,spring由于无法创建bean而抛出一些错误消息

  • org.springframework.beans.factory.NoSuchBean定义异常:没有类型为“pl.coderstrust.dao.FooDao”的符合条件的bean可用:至少需要1个符合autowire候选条件的bean

  • 创建名为“fooServiceImpl”的bean时出错:通过字段“dao”表示的未满足的依赖项


  • 因此,我的控制器bean也无法创建: 创建名为“fooController”的bean时出错:通过字段“impl”表示的未满足的依赖项

    我没有带bean的.xml文件,只有注释。运行应用程序时,将从java类读取所有配置:

    @Configuration
    @PropertySource("classpath:db.properties")
    @EnableTransactionManagement
    @ComponentScans(value = {
        @ComponentScan("pl.coderstrust.hibernate"),
        @ComponentScan("pl.coderstrust.serv"),
        @ComponentScan("pl.coderstrust.dao"),
    })
    public class AppConfig {
    
      @Autowired
      private Environment env;
    
      @Bean
      public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        return dataSource;
      }
    
      @Bean
      public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(getDataSource());
    
        Properties props = new Properties();
        props.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
        props.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
        props.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
    
        factoryBean.setHibernateProperties(props);
        factoryBean.setAnnotatedClasses(Invoice.class);
        return factoryBean;
      }
    
      @Bean
      public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
      }
    }
    
    我的SessionFactorybean也没有被创建。BeanCreationException:创建在pl.coderstrust.hibernate.AppConfig中定义的名为“getSessionFactory”的bean时出错:调用init方法失败; 我读了一些关于stackoverflow的答案,以及一些教程,其中一些说注释应该添加到接口中,而不是它的实现中。我已经尝试了所有可能的选项,并根据建议添加了所有注释。 也许有人也有同样的问题,需要改变什么

    @SpringBootApplication(scanBasePackages = "pl.coderstrust")
    public class Application {
    
      public static void main(String[] args) {
    
        AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);
        InvoiceServiceImpl service = context.getBean(InvoiceServiceImpl.class);
    
        SpringApplication.run(Application.class);
    
        LocalDate expectedDate = LocalDate.now();
        Counterparty expectedCounterparty
            = CounterpartyBuilder.builder().withCompanyName("LPD").build();
        int expectedNumberOfItem = 1;
        String expectedDescription = "test";
        BigDecimal expectedAmount = BigDecimal.TEN;
        BigDecimal expectedVatAmount = BigDecimal.ONE;
        Vat expectedVat = Vat.VAT_23;
        InvoiceBody invoiceBody = new InvoiceBody(expectedDate, expectedCounterparty, Arrays.asList(
            new InvoiceItem(expectedDescription, expectedNumberOfItem, expectedAmount,
                expectedVatAmount, expectedVat)));
        service.add(invoiceBody);
    
      }
    }
    

    如何加载AppConfig类?@Plog AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);出于兴趣,你到底想做什么?只是保存一个JPA实体?如果是这样,只需添加spring数据jpa启动器,扩展Crudepository接口并调用实体上的save。不必担心会话工厂等,您可以展示您的主要Spring引导类吗?您究竟为什么要自己创建应用程序上下文?Spring Boot已经做到了,请删除该代码。另外,
    @EnableTransactionManagement
    不需要由Spring Boot自动启用。您正在努力避免使用类路径上的框架。如何加载AppConfig类?@Plog AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);出于兴趣,你到底想做什么?只是保存一个JPA实体?如果是这样,只需添加spring数据jpa启动器,扩展Crudepository接口并调用实体上的save。不必担心会话工厂等,您可以展示您的主要Spring引导类吗?您究竟为什么要自己创建应用程序上下文?Spring Boot已经做到了,请删除该代码。另外,
    @EnableTransactionManagement
    不需要由Spring Boot自动启用。您正在努力避免使用类路径上的框架。
    @SpringBootApplication(scanBasePackages = "pl.coderstrust")
    public class Application {
    
      public static void main(String[] args) {
    
        AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);
        InvoiceServiceImpl service = context.getBean(InvoiceServiceImpl.class);
    
        SpringApplication.run(Application.class);
    
        LocalDate expectedDate = LocalDate.now();
        Counterparty expectedCounterparty
            = CounterpartyBuilder.builder().withCompanyName("LPD").build();
        int expectedNumberOfItem = 1;
        String expectedDescription = "test";
        BigDecimal expectedAmount = BigDecimal.TEN;
        BigDecimal expectedVatAmount = BigDecimal.ONE;
        Vat expectedVat = Vat.VAT_23;
        InvoiceBody invoiceBody = new InvoiceBody(expectedDate, expectedCounterparty, Arrays.asList(
            new InvoiceItem(expectedDescription, expectedNumberOfItem, expectedAmount,
                expectedVatAmount, expectedVat)));
        service.add(invoiceBody);
    
      }
    }