我有以下 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 配置文件中会很不方便。有没有办法将大量的 props 移到一个单独的资源文件中并应用它来加载 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>
关于java - 是否可以将属性定义从配置移动到资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27837489/