PropertyOverrideConfigurer 和 PropertyPlaceholderConfigurer 有什么区别?
What is difference between PropertyOverrideConfigurer and PropertyPlaceholderConfigurer?
在Spring框架中使用PropertyOverrideConfigurer
和PropertyPlaceholderConfigurer
有什么区别?我找不到这两个 类.
之间的任何实质性区别
PropertyOverrideConfigurer :
"Property resource configurer that overrides bean property values in
an application context definition. It pushes values from a properties
file into bean definitions."
它允许您覆盖 bean 的某些值,这意味着您可以从 属性 文件
中定义的属性覆盖 spring bean 的某些值
声明:
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location" value="classpath:myproperties.properties" />
</bean>
<bean id="person" class="com.sample.Employee" >
<property name="name" value="Dugan"/>
<property name="age" value="50"/>
</bean>
myproperties.properties:
person.age=40
person.name=Stanis
所以当你加载 bean 时
Employee e = (Employee)context.getBean(Employee.class);
e.getAge() => 40
e.getName() => "Stanis"
PropertyPlaceholderConfigurer :
resolves ${...} placeholders against local properties and/or system
properties and environment variables.
它允许您解析 bean 定义中的 ${..} 占位符,它还检查系统属性的值。可以使用 systemPropertiesMode
控制此行为
- never (0): 从不检查系统属性
- fallback (1): 如果无法解析则检查系统属性
指定的属性文件。这是默认设置。
- 覆盖(2):首先检查系统属性,然后再尝试
指定的属性文件。这允许系统属性覆盖
任何其他 属性 来源。
配置
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="username" value="root" />
<property name="password" value="password" />
<property name="systemPropertiesMode" value="0" />
</bean>
将 'dataSource' 属性移动到 属性 个文件
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=password
然后用占位符引用他们 =>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
在Spring框架中使用PropertyOverrideConfigurer
和PropertyPlaceholderConfigurer
有什么区别?我找不到这两个 类.
PropertyOverrideConfigurer :
"Property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions."
它允许您覆盖 bean 的某些值,这意味着您可以从 属性 文件
中定义的属性覆盖 spring bean 的某些值声明:
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location" value="classpath:myproperties.properties" />
</bean>
<bean id="person" class="com.sample.Employee" >
<property name="name" value="Dugan"/>
<property name="age" value="50"/>
</bean>
myproperties.properties:
person.age=40
person.name=Stanis
所以当你加载 bean 时
Employee e = (Employee)context.getBean(Employee.class);
e.getAge() => 40
e.getName() => "Stanis"
PropertyPlaceholderConfigurer :
resolves ${...} placeholders against local properties and/or system properties and environment variables.
它允许您解析 bean 定义中的 ${..} 占位符,它还检查系统属性的值。可以使用 systemPropertiesMode
控制此行为- never (0): 从不检查系统属性
- fallback (1): 如果无法解析则检查系统属性 指定的属性文件。这是默认设置。
- 覆盖(2):首先检查系统属性,然后再尝试 指定的属性文件。这允许系统属性覆盖 任何其他 属性 来源。
配置
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="username" value="root" />
<property name="password" value="password" />
<property name="systemPropertiesMode" value="0" />
</bean>
将 'dataSource' 属性移动到 属性 个文件
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=password
然后用占位符引用他们 =>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>