What are the differences between using PropertyOverrideConfigurer
and PropertyPlaceholderConfigurer
in the Spring framework? I'm unable to find any solid difference between these 2 classes.
PropertyOverrideConfigurer :
"Property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions."
it allows you to override some values that beans take, means you can override some values of spring beans from properties defined in property file
declare:
<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
so when you load the 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.
it allows you to resolve ${..} placeholders in bean definitions, it also checks for System properties for values. This behavior can be controlled with systemPropertiesMode
to configure
<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>
move the 'dataSource' properties to property files
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=password
then refer them with place holders =>
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With