在 Spring 中读取 2 属性 个具有相同变量名的文件
Reading 2 property files having same variable names in Spring
我正在使用 Spring xml.xml 中的以下条目阅读 属性 文件
<context:property-placeholder
location="classpath:resources/database1.properties,
classpath:resources/licence.properties"/>
我正在使用 xml 条目或使用 @Value
注释将此值注入变量。
<bean id="myClass" class="MyClass">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="name" value="${database.name}" />
</bean>
我想添加一个新的 属性 文件 (database2.properties
),它有几个与 database1.properties.
相同的变量名
database1.properties:
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.10/
database.name=dbname
database2.properties:
database.url=jdbc:mysql://192.168.1.50/
database.name=anotherdbname
database.user=sampleuser
您可以在两个 属性 文件中看到很少 属性 变量具有相同的名称,例如 database.url
、database.name
。
Is it possible to inject database.url
of database2.properties?
Or I have to change variable names?
谢谢。
可能有一个解决方案,类似于您想要实现的目标。检查此问题的第二个答案:Multiple properties access。它基本上解释了如何使用您定义的另一个表达式来访问第二个文件的属性。
否则,最简单的解决方案就是更改键值(变量名称)。
你可以配置两个 PropertyPlaceholderConfigurer
. Usually there's only one instance that serves out all the properties, however, if you change the placeholderPrefix
你可以使用两个实例,比如
<bean id="firstPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:resources/database1.properties,
classpath:resources/licence.properties" />
<property name="placeholderPrefix" value="${db1."/>
</bean>
<bean id="secondPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:resources/database2.properties" />
<property name="placeholderPrefix" value="${db2."/>"
</bean>
然后您将访问您的属性,如 ${db1.database.url}
或 ${db2.database.url}
你迟早会切换到 Spring Boot。所以使用 Spring Boot 你可以拥有这样的 POJO:
public class Database {
@NotBlank
private String driver;
@NotBlank
private String url;
@NotBlank
private String dbname;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDbname() {
return dbname;
}
public void setDbname(String dbname) {
this.dbname = dbname;
}
}
并使用@ConfigurationProperties 来填充它:
@Bean
@ConfigurationProperties(locations="classpath:database1.properties", prefix="driver")
public Database database1(){
return new Database();
}
@Bean
@ConfigurationProperties(locations="classpath:database2.properties", prefix="driver")
public Database database2(){
return new Database();
}
缺点是它是可变的。使用 Lombok 库,您可以消除讨厌的 getter 和 setter。
我正在使用 Spring xml.xml 中的以下条目阅读 属性 文件
<context:property-placeholder
location="classpath:resources/database1.properties,
classpath:resources/licence.properties"/>
我正在使用 xml 条目或使用 @Value
注释将此值注入变量。
<bean id="myClass" class="MyClass">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="name" value="${database.name}" />
</bean>
我想添加一个新的 属性 文件 (database2.properties
),它有几个与 database1.properties.
database1.properties:
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.10/
database.name=dbname
database2.properties:
database.url=jdbc:mysql://192.168.1.50/
database.name=anotherdbname
database.user=sampleuser
您可以在两个 属性 文件中看到很少 属性 变量具有相同的名称,例如 database.url
、database.name
。
Is it possible to inject
database.url
of database2.properties?Or I have to change variable names?
谢谢。
可能有一个解决方案,类似于您想要实现的目标。检查此问题的第二个答案:Multiple properties access。它基本上解释了如何使用您定义的另一个表达式来访问第二个文件的属性。 否则,最简单的解决方案就是更改键值(变量名称)。
你可以配置两个 PropertyPlaceholderConfigurer
. Usually there's only one instance that serves out all the properties, however, if you change the placeholderPrefix
你可以使用两个实例,比如
<bean id="firstPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:resources/database1.properties,
classpath:resources/licence.properties" />
<property name="placeholderPrefix" value="${db1."/>
</bean>
<bean id="secondPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:resources/database2.properties" />
<property name="placeholderPrefix" value="${db2."/>"
</bean>
然后您将访问您的属性,如 ${db1.database.url}
或 ${db2.database.url}
你迟早会切换到 Spring Boot。所以使用 Spring Boot 你可以拥有这样的 POJO:
public class Database {
@NotBlank
private String driver;
@NotBlank
private String url;
@NotBlank
private String dbname;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDbname() {
return dbname;
}
public void setDbname(String dbname) {
this.dbname = dbname;
}
}
并使用@ConfigurationProperties 来填充它:
@Bean
@ConfigurationProperties(locations="classpath:database1.properties", prefix="driver")
public Database database1(){
return new Database();
}
@Bean
@ConfigurationProperties(locations="classpath:database2.properties", prefix="driver")
public Database database2(){
return new Database();
}
缺点是它是可变的。使用 Lombok 库,您可以消除讨厌的 getter 和 setter。