使用带有Annotated setSessionFactory的Hibernate交互器

时间:2014-11-04 10:19:16

标签: hibernate spring-mvc

我正在开发一个使用Hibernate作为ORM工具的Spring-MVC应用程序。我想使用拦截器来执行项目中的一些任务。我有一个NotesDAOImpl文件,它有DAO代码。 现在,当我想使用拦截器时,我必须使用Configuration和StandardServiceRegistryBuilder创建sessionFactory。

请注意我解释这个问题的奇怪方式,我不知道如何描述。

当我在bean中使用sessionFactory时,这给了我一个问题。我想我会粘贴一些代码来说清楚:

NotesDAOImpl:

@Transactional
@Repository
public class NotesDAOImpl implements NotesDAO{

    @Autowired
    private static SessionFactory sessionFactory=buildSessionFactory();

    private static ServiceRegistry serviceRegistry;


    public SessionFactory getSessionFactory() {
        sessionFactory = buildSessionFactory();
        return sessionFactory;
    }

    public void shudtown(){
        getSessionFactory().close();
    }

    private static SessionFactory buildSessionFactory(){
        Configuration configuration = new Configuration().setInterceptor(new NotesInterceptor());
        configuration.configure();

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }

现在如果我尝试在NotesDAOImpl代码中集成buildSessionFactory方法和getSessionFactory()方法,那么bean(下面)会抛出错误(下面的错误),因为它找不到sessionFactory的引用。我找不到任何可以使用我的代码的拦截器实现。有什么想法吗?

豆子:

<mvc:annotation-driven/>

<mvc:default-servlet-handler/>

<resources mapping="/resources/**" location="/resources/" />

<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <beans:property name="driverClassName" value="org.postgresql.Driver"  />
    <beans:property name="url"
        value="jdbc:postgresql://localhost:5432/Person2"/>
    <beans:property name="username" value="postgres" />
    <beans:property name="password" value="asayhk2787" />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"  />
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>com.journaldev.spring.model.Person</beans:value>
            <beans:value>com.journaldev.spring.model.Notes</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
            <beans:prop key="connection.pool_size">200</beans:prop>
            <beans:prop key="c3p0.max_size">200</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<beans:bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory"
                    ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

<!-- Person beans being here -->
<beans:bean id="personDAO" class="com.journaldev.spring.dao.PersonDAOImpl">
    <beans:property name="sessionFactory"
        ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="personService"
    class="com.journaldev.spring.service.PersonServiceImpl">
    <beans:property name="personDAO" ref="personDAO"></beans:property>
</beans:bean>

<!-- Notes bean begin here -->
<beans:bean id="notesDAO" class="com.journaldev.spring.dao.NotesDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>


<beans:bean id="notesService" class="com.journaldev.spring.service.NotesServiceImpl">
    <beans:property name="notesDAO" ref="notesDAO"/>
</beans:bean>

<beans:bean id="LoginServiceImpl" class="com.journaldev.spring.service.LoginServiceImpl"/>
<context:component-scan base-package="com.journaldev.spring" />

<!--  Beans for classes end here -->

错误代码:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notesDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)

我应该更换豆子吗?我尝试将其更改为hibernate4TransactionManager,但没有工作。有什么想法吗?

0 个答案:

没有答案