简体   繁体   English

如何动态修改Java属性?

[英]How can I modify java properties on the fly?

I'm looking the way how to control java properties during installation procedure. 我正在寻找在安装过程中如何控制Java属性的方法。

I'm have Spring Data JPA + Hibernate in my application. 我的应用程序中有Spring Data JPA + Hibernate。 I've configured jpa properties in bean. 我已经在bean中配置了jpa属性。 And move it to property file. 并将其移动到属性文件。

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
    <property name="persistenceUnitName" value="hibernatePersistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl}"/>
            <entry key="hibernate.show_sql" value="${hibernate.show.sql}" />
            <entry key="hibernate.dialect" value="${hibernate.dialect}" />
        </map>
    </property>
</bean>

property file: prop-local-override.properties where local is environment variable. 属性文件:prop-local-override.properties,其中local是环境变量。

hibernate.hbm2ddl=update
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show.sql=true

So, everything works fine. 因此,一切正常。 But I want to change hibernate.hbm2ddl during installation process. 但是我想在安装过程中更改hibernate.hbm2ddl。 I use rpm package for installation my app. 我使用rpm包安装我的应用程序。 For example, I want to set this property to "create" when app is installing. 例如,我要在安装应用程序时将此属性设置为“创建”。 And return to update after each restart. 并在每次重新启动后返回更新。

Looks like -Dhibernate.hbm2ddl=update works fine. 看起来-Dhibernate.hbm2ddl = update工作正常。 But it's manual work. 但这是手工工作。 Does anyone has some idea how to make it automatically, without big tools like Puppet? 没有人知道没有Puppet这样的大工具如何自动进行制作吗?

Thanks, Sergii K. 谢谢,Sergii K.

Never tried changing this dynamically before, but you can access that property map from your entity manager: 以前从未尝试过动态更改此属性,但是您可以从实体管理器访问该属性映射:

entityManager.getEntityManagerFactory().getProperties() entityManager.getEntityManagerFactory()。的GetProperties()

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

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