使用Spring和Hibernate的基于XML的配置

时间:2016-10-06 09:00:55

标签: spring hibernate

我有这个基于xml的配置

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
    <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/Hibernate" />
        <beans:property name="username" value="postgres" />
        <beans:property name="password" value="password" />
    </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://192.168.0.9:5435/HGCTEST?autoReconnect=true" />
        <beans:property name="username" value="AppDev" />
        <beans:property name="password" value="hgcadmin" />
    </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.bankdeposit.model.Check</beans:value>
                <beans:value>com.bankdeposit.model.Unit</beans:value>

            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>

                <beans:prop key="hibernate.c3p0.min_size">5</beans:prop>
                <beans:prop key="hibernate.c3p0.max_size">20</beans:prop>
                <beans:prop key="hibernate.c3p0.timeout">300</beans:prop>
                <beans:prop key="hibernate.c3p0.max_statements">50</beans:prop>
                <beans:prop key="hibernate.c3p0.idle_test_period">3000</beans:prop>

            </beans:props>
        </beans:property>
    </beans:bean>

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

    <beans:bean id="checkDAO" class="com.bankdeposit.dao.CheckDAOImpl">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

    <beans:bean id="checkService" class="com.bankdeposit.service.CheckServiceImpl">
        <beans:property name="checkDAO" ref="checkDAO"></beans:property>
    </beans:bean>

    <beans:bean id="unitDAO" class="com.bankdeposit.dao.UnitDAOImpl">
        <beans:property name="sessionFactory2" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

    <beans:bean id="unitService" class="com.bankdeposit.service.UnitServiceImpl">
        <beans:property name="unitDAO" ref="unitDAO"></beans:property>
    </beans:bean>



    <context:component-scan base-package="com.bankdeposit" />

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




</beans:beans>

在我尝试使用以下代码注册我的其他bean之后:

<beans:bean id="unitDAO" class="com.bankdeposit.dao.UnitDAOImpl">
    <beans:property name="sessionFactory2" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

<beans:bean id="unitService" class="com.bankdeposit.service.UnitServiceImpl">
    <beans:property name="unitDAO" ref="unitDAO"></beans:property>
</beans:bean>

我收到以下消息的错误

org.springframework.beans.factory.BeanCreationException:创建名称为&#39; unitDAO&#39;的bean时出错在ServletContext资源[/WEB-INF/spring/bdservlet/servlet-context.xml]中定义:设置属性值时出错;

如何注册在xml中创建的新Bean?

这是我的UnitDAOImpl

@Repository
public class UnitDAOImpl implements UnitDAO {

    private static final Logger logger = LoggerFactory.getLogger(UnitDAOImpl.class);

    private SessionFactory sessionFactory;

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

    @Override
    public List<Unit> listUnits() {
        Session session = this.sessionFactory.getCurrentSession();

        // query that hase no where clause
        List<Unit> unitList = session.createQuery("from Unit order by code asc").list();
        for (Unit u : unitList) {
            logger.info("unit List::" + u);
        }

        return unitList;

    }

}

2 个答案:

答案 0 :(得分:1)

您需要显示您的类UnitDAOImpl,但只是怀疑:您的类UnitDAOImpl是否具有名为&#34; sessionFactory2&#34;的实际属性?如果你的属性的setter和getter方法是&#34; getSessionFactory()&#34;和&#34; setSessionFactory()&#34; (或者如果它基于成员本身并且您的变量被调用&#34; sessionFactory&#34;那么在您的配置中为UnitDAOImpl <beans:property name="sessionFactory2"更改&#34; sessionFactory2&#34;到&#34; sessionFactory&#34 ;

答案 1 :(得分:0)

您的配置确实存在多个问题。

i)最明显的是@Michael Gantman在他的回答中提到的,由于某种原因,你连接了似乎不存在于某处的sessionFactory2属性。

ii)您的第二个明显问题是您可能会扫描相同的包两次,因此对于您的UnitDAOImpl,将会因为此行而创建第一个参考

<beans:bean id="unitDAO" class="com.bankdeposit.dao.UnitDAOImpl"> <beans:property name="sessionFactory2" ref="hibernate4AnnotatedSessionFactory" /> </beans:bean

这将是一个正确配置的spring bean,如果您将属性名称更改为sessionFactory,该属性也将被连接。

现在将创建第二个实例,因为这行

<context:component-scan base-package="com.bankdeposit" /> 并且因为你没有在构造函数级别隐式设置@Autowired注释,sessionFactory属性不会被连线,所以可能在调用DAO方法时你会得到一个NPE。

另外要确认一下,您可以将其添加到UnitDAOImpl构造函数方法中,以检查它是否会被调用两次:

public UnitDAOImpl() { System.out.println("Constructor Called"); }