Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between PropertyOverrideConfigurer and PropertyPlaceholderConfigurer?

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.

like image 225
Siddharth Avatar asked Oct 17 '16 06:10

Siddharth


Video Answer


1 Answers

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

  • never (0): Never check system properties
  • fallback (1): Check system properties if not resolvable in the specified properties files. This is the default.
  • override (2): Check system properties first, before trying the specified properties files. This allows system properties to override any other property source.

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>
like image 164
kuhajeyan Avatar answered Oct 10 '22 17:10

kuhajeyan