我有一个带有 spring 3 和 hibernate 框架的标准 Web 应用程序。我有包含数据库连接数据的 applicationContext.xml 和 hibernate.cfg.xml 文件:
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/webapp</property>
<property name="hibernate.connection.username">webapp</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
</session-factory>
</hibernate-configuration>
...
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/webapp" />
<property name="username" value="webapp" />
<property name="password" value="password" />
</bean>
现在我需要使用动态数据库名称更改此应用程序,如何在运行时设置它,例如在 ServletContextListener.contextInitialized 中?