Spring Boot 应用程序中的 sessionFactory 不会创建

sessionFactory in SpringBoot application won't create

我正在使用 SpringBoot 并且正在尝试连接到 oracle 数据库。 按照教程,我编写了以下导致以下异常的代码。 在执行 factoryBean.afterPropertiesSet(); 时,我不明白问题出在哪里;它崩溃了。

有什么建议吗?

@Configuration
@ConfigurationProperties("oracle")
@EnableAutoConfiguration(exclude = { //
        DataSourceAutoConfiguration.class, //
        DataSourceTransactionManagerAutoConfiguration.class, //
        HibernateJpaAutoConfiguration.class })
public class BeanFactory_EX{

    @Autowired
    private Environment env;

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        // See: application.properties
        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;
    }

    @Autowired
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
        Properties properties = new Properties();

        // See: application.properties
        properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
        properties.put("show_sql", env.getProperty("spring.jpa.show-sql"));
        properties.put("current_session_context_class", env.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
        properties.put("hibernate.temp.use_jdbc_metadata_defaults",false);

        // Fix Postgres JPA Error:
        // Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

        // Package contain entity classes
        factoryBean.setPackagesToScan(new String[] { "" });
        factoryBean.setDataSource(dataSource);
        factoryBean.setHibernateProperties(properties);
        factoryBean.afterPropertiesSet();
        //
        SessionFactory sf = factoryBean.getObject();
        System.out.println("## getSessionFactory: " + sf);
        return sf;
    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

        return transactionManager;
    }

}

例外情况是:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-09-28 12:24:28.144 ERROR 11160 --- [           main] o.s.boot.SpringApplication           : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/test/BeanFactory_EX.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.TypeNotPresentException: Type org.threeten.bp.Instant not present   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=11=](AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]  at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]    at com.test.Application.main(Application.java:16) [classes/:na] Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.TypeNotPresentException: Type org.threeten.bp.Instant not present  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]  at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]    ... 18 common frames omitted Caused by: java.lang.TypeNotPresentException: Type org.threeten.bp.Instant not present     at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117) ~[na:1.8.0_181]     at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125) ~[na:1.8.0_181]   at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49) ~[na:1.8.0_181]  at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68) ~[na:1.8.0_181]     at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138) ~[na:1.8.0_181]   at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49) ~[na:1.8.0_181]  at sun.reflect.generics.repository.ClassRepository.getSuperInterfaces(ClassRepository.java:108) ~[na:1.8.0_181]     at java.lang.Class.getGenericInterfaces(Class.java:913) ~[na:1.8.0_181]     at org.hibernate.cfg.AttributeConverterDefinition.extractAttributeConverterParameterizedType(AttributeConverterDefinition.java:157) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]     at org.hibernate.cfg.AttributeConverterDefinition.<init>(AttributeConverterDefinition.java:114) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]     at org.hibernate.cfg.AttributeConverterDefinition.from(AttributeConverterDefinition.java:93) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]    at org.hibernate.cfg.AttributeConverterDefinition.from(AttributeConverterDefinition.java:73) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]    at org.hibernate.cfg.Configuration.addAttributeConverter(Configuration.java:766) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]    at org.springframework.orm.hibernate5.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:331) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:512) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at com.test.BeanFactory_EX.getSessionFactory(BeanFactory_EX.java:64) ~[classes/:na]     at com.test.BeanFactory_EX$$EnhancerBySpringCGLIB$$f431f7a1.CGLIB$getSessionFactory(<generated>) ~[classes/:na]   at com.test.BeanFactory_EX$$EnhancerBySpringCGLIB$$f431f7a1$$FastClassBySpringCGLIB$cfc31df.invoke(<generated>) ~[classes/:na]    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]     at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:365) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]     at com.test.BeanFactory_EX$$EnhancerBySpringCGLIB$$f431f7a1.getSessionFactory(<generated>) ~[classes/:na]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]     at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]  ... 19 common frames omitted Caused by: java.lang.ClassNotFoundException: org.threeten.bp.Instant   at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181]   at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181]    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_181]    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181]    at java.lang.Class.forName0(Native Method) ~[na:1.8.0_181]  at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_181]  at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114) ~[na:1.8.0_181]     ... 44 common frames omitted

Disconnected from the target VM, address: '127.0.0.1:51228', transport: 'socket'

Process finished with exit code 1

编辑:

在我通过添加缺失的依赖项纠正崩溃后,我仍然无法为 sessionFactory 提供数据。这是 class:

@Repository
@Transactional
public class ConnectionDAO {

    @Autowired
    private SessionFactory sessionFactory;


    public Customer getCustomer(String name){



        return new Customer("","");
    }

    public Boolean setCustomer(String name, String surname){
        /*
         *
         */
        return false;
    }

}

您还缺少:org.threeten.bp.Instant

因此将此依赖项添加到您的构建中(使用适当的版本更新):

<dependency> 
      <groupId>org.threeten</groupId> 
      <artifactId>threetenbp</artifactId> 
      <version>1.3.3</version> 
</dependency>