How to read properties file in Java

No Comments

How to read properties file in Java? Its pretty simple and there are numerous way to do that. In this article I will tell you how to read properties file in conventional Java techniques and in Spring framework technique.

To read properties file in Java and get the fields from it, use the following technique. Here, we read the properties file as a stream and loads to the Properties class and use the getProperty() method to get the value from the properties. This is conventional java technique to read properties file and get the data, you can use this method to read properties file irrespective of whether you are using any java framework or not.

public Properties getBankConfigProps() {

		// Reading properties file to set Default values
		Properties bankConfig = new Properties();
		try {
			bankConfig.load(BankUtility.class.getClassLoader().getResourceAsStream("conf/configuration.properties"));
		} catch (IOException e) {
			System.out.println("Exception in getBankConfigProps: "+e);
		}
		return bankConfig;
	}

	public String getClubIdFromProps()
	{
		/* Reading properties file in Java */
		Properties bankConfig;
		bankConfig = getBankConfigProps();
		String strClubId = bankConfig.getProperty("clubId");
		System.out.println("club id is "+strClubId);
		return strClubId;
	}

And of course you should have conf/configuration.properties

clubId=northclub
someotherfields1=someothervalues1
someotherfields2=someothervalues2

But, if you are using spring dependancy injection/ Inversion of control(IOC) framework in your project, then there are much better ways to load properties file in to your java application. You can inject the properties directly to your class with spring’s dependency injection.

How to read properties file in Spring versions prior to spring 3.0

This is a really cool feature from Spring framework, you dont need to read from properties file on your own, instead spring will inject the values for you wherever you want. You just need to add a class variable to which the value should get injected(expirationOffset and expirationOffsetString)and a setter method. See the below example, spring will handle the data type also. You can declare the variable(to which you need the value to get injected from properties file) as String or int, spring will do the rest for you, you dont need to typecast it. Here, in this example its trying to load values from properties file to expirationOffset and expirationOffsetString. So your Java class will look something like this.

BankServiceImpl.java

public class BankServiceImpl implements BankService {
	private int expirationOffset;
	private String expirationOffsetString;

	public void setExpirationOffset(int expirationOffset) {
		this.expirationOffset = expirationOffset;
	}

	public void setExpirationOffsetString(String expirationOffsetString) {
		this.expirationOffsetString = expirationOffsetString;
	}

And our ApplicationContext.xml will look something like this

<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>file:conf/configuration.properties</value>
		</property>
	</bean>
	<bean id="bankService" class="com.blah.blah.BankServiceImpl">
		<property name="bankDAO">
			<ref bean="bankDAO" />
		</property>
		<property name="expirationOffset">
			<value>${xprnOffset}</value>
		</property>
		<property name="expirationOffsetString">
			<value>${xprnOffsetProp}</value>
		</property>
	</bean>
</beans>

<property name=”expirationOffsetString”><value>${xprnOffsetProp}</value></property> using this configurations you are saying spring framework that you need to get value of xprnOffsetProp from properties file get injected to the member variable expirationOffsetString.
<property name=”expirationOffset”><value>${xprnOffset}</value></property>using this configurations you are saying spring framework that you need to get value of xprnOffset from properties file get injected to the member variable expirationOffset.

And your properties file will look something like this

conf/configuration.properties

xprnOffset= 7
xprnOffsetProp= Hello
someotherfields1=someothervalues1
someotherfields2=someothervalues2

So when you run this application, while instantiating BankServiceImpl class, spring framework will inject expirationOffset and expirationOffsetString for you.

How to read properties file in Spring versions 3.0 +

In spring 3.0+ versions, its more easy, you don’t need to do all this setter method circus, instead you can use autowiring and annotations and all new Spring Expression Language (SpEL). you dont need to define the bean configurations, instead <context:component-scan base-package=”com.blah.blah”/> will take care of that.

So your ApplicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd

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">

    <context:annotation-config/>
    <context:component-scan base-package="com.blah.blah"/>
    <context:property-placeholder location="conf/config.properties"/>
</beans>

and in the java class where you intend to get the value injected by annotations, add this

private @Value("${xprnOffset}") int expirationOffset;
private @Value("${xprnOffsetProp}") int expirationOffsetString;

use the same conf/configuration.properties

xprnOffset= 7
xprnOffsetProp= Hello
someotherfields1=someothervalues1
someotherfields2=someothervalues2

your member variable expirationOffset will get injected with the value 7.
your member variable expirationOffsetString will get injected with the value “Hello”.

Garbage Collection or Memory DeAllocation in Java

1 Comment

Garbage Collection Memory DeAllocation in Java

In Java, an object which is no longer referred by any reference variable will automatically be removed from the memory, this process is known as Garbage Collection. Garbage Collection is automatically done by Java Virtual Machine (JVM), the automatic Garbage Collection of Java de-allocates the dynamic memory when this memory is no more used by the program. Thus the Garabage collection feature of Java relieves the programmer from the overhead of memory de-allocation.(A major difficulty in dynamic memory allocation in C/C++ was that the programmer is responsible for de-allocating the dynamic memory at the right time. Even though experienced programmers can do this very well, beginners and average programmers often miss the statements for de-allocation which leads to memory-leak in many systems.)

How Garbage collection in Java works

If a reference variable is declared within a function, the reference is invalidated soon as the function call ends. Or programmer can explicitly set the reference variable to null to indicate that the referred object is no longer in use. And then Garbage collector will claim the memory allotted for that.

Please note: Primitive data types are not objects and they cannot be assigned null.

What are JEE containers?

1 Comment

There are different kind of JEE containers. Take a look at the following JEE containers.

Application client container
Application client container is the one which is responsible for managing the application client components. Both application client and its container resides at the client machine and are executed on the client end. Example of application container is JVM.

Applet container
Applet container is the container to manage the execution of applets. Examples of an applet container include Web browser and applet viewer.

Enterprise JavaBeans (EJB) container
Enterprise JavaBeans (EJB) container is used to manage the execution of EJB component of the J2EE application. Both the EJBs and its container run on the J2EE server. J2EE server provides the EJB container, and an example of J2EE server is WebLogic.

Web container
Web container is used to manage the execution of the Servlets and the JSP. Both the Web components and its container run on the J2EE server. An example of a web container is Tomcat.

Responsibilities of JEE containers
  • Life cycle Management- Creating and deleting components
  • Persistence- Saving information in the components
  • Naming- Recording the name of a server component and locating it for the client.
  • Deployment- Placing components in the server
  • Messaging- Exchanging information between components.

Difference between Class and object in Java?

No Comments

 

Java Object and Class Difference between Object and Class

Java Object and Class Difference

Difference between Class and object in Java

In Java,  Class is a blue print used to create objects. In other words, a Class is a template that defines the methods (Functions) and Members (variables) to be included in a particular kind of Object.

Here, “Student” is a class. And “Jenna” is an object of the class “Student”. And “John” is another object of the class “Student”. A Class is a template for an object, a user-defined datatype that contains the variables and methods in it.

A class defines the abstract characteristics of a thing (object), including its characteristics (its attributes, fields or properties) and the thing’s behaviors (the things it can do, or methods, operations or features). That is, Class is a blue print used to create objects. In other words, each object is an instance of a class. One class can have multiple number of instances. (There can be Number of instances of the class “Student”, like “Jenna”, “John” etc).

A class is a predefined frame of an object, in which its mentioning what all variables and methods an object (object of that class) can have.