db.properties vs persistence.xml哪个更好?

时间:2021-08-07 16:06:21

Recently I started a maven project to build an application for integrating

最近我开始了一个maven项目来构建一个集成应用程序

Spring, JPA, JSF

Spring,JPA,JSF

But in the automatically generated folder structure I can see one file named as

但在自动生成的文件夹结构中,我可以看到一个名为的文件

db.properties

db.properties

and also I have one

而且我也有一个

persistence.xml

persistence.xml中

Now my question is that Database connection can be defined in either of these files, Can anybody tell me 1. Which way is better and why ? 2. Why there is db.properties file automatically generated while I already have persistence.xml ?

现在我的问题是数据库连接可以在这些文件中定义,任何人都可以告诉我1.哪种方式更好,为什么? 2.为什么在我已经拥有persistence.xml时会自动生成db.properties文件?

2 个解决方案

#1


0  

db.properties file is like messages.properties which is used to define key value pair. And after that we will use keys in expression language. So configurations will only be done in

db.properties文件类似于messages.properties,用于定义键值对。之后我们将使用表达式语言中的键。所以配置只会在

persistence.xml or dataSource.xml

persistence.xml或dataSource.xml

whichever is preferred choice but the values we will take from db.properties in the form of expression language eg.

无论哪个是首选,但我们将以表达式语言的形式从db.properties中获取值。

 driverClassName=com.mysql.jdbc.Driver

this is an entry in your db.properties. and you will use it in persistence.xml as follows.

这是db.properties中的一个条目。您将在persistence.xml中使用它,如下所示。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driverClassName}" />

#2


0  

I assume, from the fact that you mention JSF, that you are building a web application for deployment to an application server. I also caveat this question in that I don't know about db.properties or where it comes from.

从您提到JSF的事实来看,我假设您正在构建用于部署到应用程序服务器的Web应用程序。我还提醒我这个问题,因为我不知道db.properties或它来自哪里。

When deploying to an application server, it is always best to configure your database connections in the container and expose them to the application via JNDI. This allows the container to manage connection pooling and credentials and keeps this information out of your WAR/EAR files. It also ensures that your WAR/EAR files are agnostic to the particular database instance, so can be deployed to a container in any environment without modification.

部署到应用程序服务器时,最好在容器中配置数据库连接,并通过JNDI将它们公开给应用程序。这允许容器管理连接池和凭据,并将此信息保留在WAR / EAR文件之外。它还确保您的WAR / EAR文件与特定数据库实例无关,因此可以在任何环境中部署到容器而无需修改。

Therefore, I recommend against configuring your datasource in persistence.xml.

因此,我建议不要在persistence.xml中配置数据源。

See also Difference between configuring data source in persistence.xml and in spring configuration files which is a similar question- the accepted answer there expresses the solution in more detail.

另请参阅在persistence.xml和spring配置文件中配置数据源的区别,这是一个类似的问题 - 接受的答案更详细地表达了解决方案。

#1


0  

db.properties file is like messages.properties which is used to define key value pair. And after that we will use keys in expression language. So configurations will only be done in

db.properties文件类似于messages.properties,用于定义键值对。之后我们将使用表达式语言中的键。所以配置只会在

persistence.xml or dataSource.xml

persistence.xml或dataSource.xml

whichever is preferred choice but the values we will take from db.properties in the form of expression language eg.

无论哪个是首选,但我们将以表达式语言的形式从db.properties中获取值。

 driverClassName=com.mysql.jdbc.Driver

this is an entry in your db.properties. and you will use it in persistence.xml as follows.

这是db.properties中的一个条目。您将在persistence.xml中使用它,如下所示。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driverClassName}" />

#2


0  

I assume, from the fact that you mention JSF, that you are building a web application for deployment to an application server. I also caveat this question in that I don't know about db.properties or where it comes from.

从您提到JSF的事实来看,我假设您正在构建用于部署到应用程序服务器的Web应用程序。我还提醒我这个问题,因为我不知道db.properties或它来自哪里。

When deploying to an application server, it is always best to configure your database connections in the container and expose them to the application via JNDI. This allows the container to manage connection pooling and credentials and keeps this information out of your WAR/EAR files. It also ensures that your WAR/EAR files are agnostic to the particular database instance, so can be deployed to a container in any environment without modification.

部署到应用程序服务器时,最好在容器中配置数据库连接,并通过JNDI将它们公开给应用程序。这允许容器管理连接池和凭据,并将此信息保留在WAR / EAR文件之外。它还确保您的WAR / EAR文件与特定数据库实例无关,因此可以在任何环境中部署到容器而无需修改。

Therefore, I recommend against configuring your datasource in persistence.xml.

因此,我建议不要在persistence.xml中配置数据源。

See also Difference between configuring data source in persistence.xml and in spring configuration files which is a similar question- the accepted answer there expresses the solution in more detail.

另请参阅在persistence.xml和spring配置文件中配置数据源的区别,这是一个类似的问题 - 接受的答案更详细地表达了解决方案。