是否可以将属性定义从配置移动到资源?

时间:2015-01-08 10:09:14

标签: java spring hibernate properties configuration

我有以下sessionFactory Bean定义:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jtaTransactionManager" ref="transactionManager" />
    <property name="annotatedClasses">
        <list>
            <value>com.badmitrii.db.entity.Employee</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <!-- Here is a crowd of props -->
        </props>            
    </property>
</bean>

我认为将所需的所有属性放入spring配置文件是不合理的。有没有办法将道具群移动到一个单独的资源文件中并应用它加载bean?

1 个答案:

答案 0 :(得分:1)

您需要使用configLocations LocalSessionFactoryBean属性并将其配置到hibernate.cfg.xml文件所在的位置:

classpath:hibernate.cfg.xml

hibernate.cfg.xml将包含所有Hibernate属性:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/mydb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">admin</property>
    </session-factory>
</hibernate-configuration>