Почему я не могу зарегистрировать SessionFactory как компонент? - PullRequest
0 голосов
/ 09 апреля 2019

Я хочу зарегистрировать SessionFactory как компонент и автоматически связать его с компонентом DAO. Мне, кажется, не позволено делать это, и я не знаю, почему ..

Сначала я попробовал вот так:

@Configuration
public class MemoConfig {
    @Bean
    public MemberDao memberDao() {
        SessionFactory factory = new org.hibernate.cfg.Configuration()
                                        .configure("hibernate.cfg.xml")
                                        .addAnnotatedClass(Member.class)
                                        .addAnnotatedClass(Memo.class)
                                        .buildSessionFactory();
        return new MemberDao(factory);
    }
}

и я получил ошибки. Итак, я попробовал другой способ:

@Configuration
public class MemoConfig {
    @Bean
    public MemberDao memberDao() {
        return new MemberDao(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {
        SessionFactory factory = new org.hibernate.cfg.Configuration()
                .configure("hibernate.cfg.xml")
                .addAnnotatedClass(Member.class)
                .addAnnotatedClass(Memo.class)
                .buildSessionFactory();
        return factory;
    }
}

и я получил те же сообщения об ошибках.

Вот сообщения об ошибках.

09-Apr-2019 14:04:48.700 심각 [http-nio-8181-exec-1] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberDao' defined in com.myMemo.app.configuration.MemoConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myMemo.app.member.MemberDao]: Factory method 'memberDao' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.myMemo.app.configuration.MemoConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1305)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1144)
...


Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at com.myMemo.app.configuration.MemoConfig.sessionFactory(MemoConfig.java:24)
    at com.myMemo.app.configuration.MemoConfig$$EnhancerBySpringCGLIB$$41a7e678.CGLIB$sessionFactory$1(<generated>)
    at com.myMemo.app.configuration.MemoConfig$$EnhancerBySpringCGLIB$$41a7e678$$FastClassBySpringCGLIB$$75fa1e22.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...