简体   繁体   English

在Spring MVC + Hibernate应用程序的某个类中获取方言

[英]Get Dialect within some class inside Spring MVC + Hibernate application

I have Hibernate Transaction manager configured inside Spring MVC controller. 我在Spring MVC控制器中配置了Hibernate Transaction Manager。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.postgresql.Driver" />
  <property name="url" value="jdbc:postgresql://127.0.0.1/doolloop2" />
  <property name="username" value="doolloop2" />
  <property name="password" value="doolloop" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
     <property name="mappingLocations">
      <list>
        <value>WEB-INF/mapping/User.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
 <props>
  <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
  <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.hbm2ddl.auto">update</prop> 
  </props>
  </property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>   

In Addition, I have some class which needs to get Hibernate Dialect inside on of it method. 另外,我有一些需要在它的方法内部获取休眠方言的类。 Is class is not Configured as bean inside Spring Framework. 是不是在Spring Framework中没有将class配置为bean。

How can I access Hibernate Dialect property from this class? 如何从此类访问Hibernate Dialect属性? I believe it should be some static class,but I don't know how can I do it. 我相信这应该是一些静态课程,但是我不知道该怎么办。 Please help. 请帮忙。

You could separate the properties from the spring config. 您可以将属性与spring config分开。 Put them in a properties file, then reference that in a PropertyPlaceholderConfigurer bean ( http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share ). 将它们放在属性文件中,然后在PropertyPlaceholderConfigurer bean( http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share )中进行引用。 Then you could inject that value into whatever bean it is that you need the value in the same way you are injecting it into the sessionFactory bean. 然后,您可以将该值注入到需要它的任何bean中,就像将其注入sessionFactory bean中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM