无法创建SessionFactory和接口实现bean

时间:2017-09-07 15:45:51

标签: java hibernate spring-boot

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

  1. org.springframework.beans.factory.NoSuchBeanDefinitionException:没有符合条件的bean' pl.coderstrust.dao.FooDao'可用:预计至少有1个符合autowire候选资格的bean。

  2. 使用名称' fooServiceImpl'创建bean时出错:通过字段' dao'表示不满意的依赖关系。


  3. public interface FooService {
          public void add(Foo body);
        }
    
    
        @Service
        public class FooServiceImpl implements FooService {
    
          @Autowired
          FooDao dao;
    
          @Transactional
          @Override
          public void add(Foo body) {
            dao.add(body);
          }
        }
    
    public interface FooDao {
      void  add(Foo body);
    }
    
    @Repository
    public class FooDaoImpl implements FooDao{
    
      @Autowired
      private SessionFactory sessionFactory;
    
      @Override
      public void add(Foo body) {
        sessionFactory.getCurrentSession().save(body);
      }
    }
    

    因此,我的控制器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;
      }
    }
    

    我的sessionFactory bean也没有被创建。 BeanCreationException:创建名为' getSessionFactory'的bean时出错在pl.coderstrust.hibernate.AppConfig中定义: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);
    
      }
    }
    

0 个答案:

没有答案