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

Is it possible to move properties definition from the configuration to the resources?

我有以下 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?

您需要使用 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>