Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Property Place Holder

PropertyPlaceholderConfigurer is a property resource configurer that resolves placeholders in bean property values of context definitions. It pick values from a properties file into bean definitions.

We can use it to externalize property values from a bean definition in a separate file using the standard Java Properties format. Doing so enables the person deploying an application to customize environment-specific properties such as database URLs and passwords, without the complexity or risk of modifying the main XML definition file or files for the container.

Let's consider the simple case where we want to read some properties that are constant variables from an external Properties file. We need an XML-based configuration, where a PropertyPlaceholderConfigurer bean must be defined. At runtime, the PropertyPlaceholderConfigurer is applied to the metadata that will replace the properties of the bean. The values to replace are specified as placeholders of the form 0.

Location of the properties files
  • By default, Spring looks for the properties files in the application's directory. So if we specify:
    <property name="location" value="WEB-INF/user.properties"/>

    Then it will find the user.properties file under WEB-INF directory of the application (in case of a Spring MVC application).
  • We can use the prefix classpath: to tell Spring loads a properties file in the application's classpath. For example:
    <property name="location" value="classpath:user.properties"/>

  • In case of a Spring MVC application, the mail.properties file should be present in the WEB-INF/classes directory (or in the source directory (src) in Eclipse IDE). Use the prefix file:/// or file: to load a properties file from an absolute path. For example:
    <property name="location" value="file:///D:/config/user.properties"/>

  • In case if you are configure multiple file, then we can use the list collection for configure multiple file. For example:
    <property name="locations" >
    <list>
    <value>"WEB-INF/user.properties"</value>
    <value>"WEB-INF/user1.properties"</value>
    </list
    </property
User.java

package propertyPlaceHolder;

public class User {
private String name;
private String password;

public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}

}
context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

<bean id="user" class="propertyPlaceHolder.User">
<property name="name" value="${name}"/>
<property name="password" value="${password}"/>
</bean>
<bean class="org.springframework.beans.factory.config
.PropertyPlaceholderConfigurer">
<property name="location">
<value>propertyPlaceHolder/user.properties</value>
</property>
</bean>
</beans>

user.properties
name=User 1
password=Password 1


Main.java

package propertyPlaceHolder;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"propertyPlaceHolder/context.xml");
User user = (User)context.getBean("user");

System.out.println("User Name is "+user.getName());
System.out.println("User Password is "+user.getPassword());
}

}

     << Previous
Next >>