在J2SE应用程序中从persistence.xml外部化凭据 [英] Externalize credentials from persistence.xml in J2SE app
问题描述
我正在编写一个使用JPA进行持久化的J2SE应用程序(无企业容器)。这是我的 persistence.xml
:
< persistence xmlns = http://java.sun.com/xml/ns/persistence
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation = http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
version =2.0>
< persistence-unit name =dbstats>
<! - TODO:为什么需要? - >
< class> dreambear.stats.data.GamePlayedEvent< / class>
< class> dreambear.stats.data.HyveMemberCountPoll< / class>
< class> dreambear.stats.data.ProfileCountPoll< / class>
< class> dreambear.stats.data.UserSession< / class>
< class> dreambear.stats.data.UsersOnlinePoll< / class>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.max_fetch_depthvalue =3/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/>
<! - TODO:外部化信息 - >
< property name =javax.persistence.jdbc.uservalue =dbstats/>
< property name =javax.persistence.jdbc.passwordvalue =*****/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:mysql://example.com/dbstats/>
< / properties>
< / persistence-unit>
< /持久性>
这是一个静态文件,编译到应用程序中。但是,我需要提取证书,以便我可以在运行时从配置参数加载它们,因为它们与例如
我以默认方式加载持久性设置:
emf = Persistence.createEntityManagerFactory(dbstats);
如何在此设置中外部化凭据?我可以在运行时生成persistence.xml文件,但这有点不好。
创建 EntityManagerFactory
:
Map< String,String> properties = new HashMap< String,String>();
properties.put(javax.persistence.jdbc.user,...);
...
emf = Persistence.createEntityManagerFactory(dbstats,properties);
I'm writing a J2SE app (no enterprise container) that uses JPA for persistence. Here is my persistence.xml
:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="dbstats">
<!-- TODO: why is this needed? -->
<class>dreambear.stats.data.GamePlayedEvent</class>
<class>dreambear.stats.data.HyveMemberCountPoll</class>
<class>dreambear.stats.data.ProfileCountPoll</class>
<class>dreambear.stats.data.UserSession</class>
<class>dreambear.stats.data.UsersOnlinePoll</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- TODO: externalize information -->
<property name="javax.persistence.jdbc.user" value="dbstats" />
<property name="javax.persistence.jdbc.password" value="*****" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://example.com/dbstats" />
</properties>
</persistence-unit>
</persistence>
It's a static file, "compiled" into the application. However, I need to extract the credentials so I can load them from config parameters at runtime, because they're different for e.g. the development and live version of the app.
I load the persistence setup in the default way:
emf = Persistence.createEntityManagerFactory("dbstats");
How can I externalize the credentials in this setup? I could generate the persistence.xml file at runtime, but that's a bit hacky.
You can provide additional properties when creating EntityManagerFactory
:
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user", ...);
...
emf = Persistence.createEntityManagerFactory("dbstats", properties);
这篇关于在J2SE应用程序中从persistence.xml外部化凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!