Showing posts with label SPRING FRAMEWORK. Show all posts
Showing posts with label SPRING FRAMEWORK. Show all posts

Monday, August 31, 2015

Autowiring without Annotation

INTRODUCTION

Spring Framework enables injecting our beans by without mentioning the dependencies with the help of auto-wiring. No need to care about the bean's properties suppose if it is another bean.

There are three ways to achieve auto-wiring in spring framework. They are
  1. By Name
  2. By Type
  3. By Constructor

In this post, I will provide examples for all these 3 types.

EXAMPLE

Create a POJO for Person.

package springcore.autowiring;

public class Person {
 private Address address;
 private Education education;

 public Address getAddress() {
   return address;
 }

 public void setAddress(Address address) {
   this.address = address;
 }

 public Education getEducation() {
   return education;
 }

 public void setEducation(Education education) {
   this.education = education;
 }
}

Create another POJO for Address.

package springcore.autowiring;

public class Address {

 private String streeName;
 private String doorNo;

 public String getStreeName() {
   return streeName;
 }

 public void setStreeName(String streeName) {
   this.streeName = streeName;
 }

 public String getDoorNo() {
   return doorNo;
 }

 public void setDoorNo(String doorNo) {
   this.doorNo = doorNo;
 }
}

Create another POJO for Education.

package springcore.autowiring;

public class Education {

 private String qualification;
 private String institute;

 public String getQualification() {
   return qualification;
 }

 public void setQualification(String name) {
   this.qualification = name;
 }

 public String getInstitute() {
   return institute;
 }

 public void setInstitute(String institute) {
   this.institute = institute;
 }

}

Now configure the beans in beans configuration xml (autowiring-beans.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">

<bean id="address" class="springcore.autowiring.Address">
 <property name="streeName" value="Gandhi Street" />
   <property name="doorNo" value="15" />
</bean>

<bean id="education" class="springcore.autowiring.Education">
  <property name="qualification" value="M.Tech" />
  <property name="institute" value="SRM University" />
</bean>

</beans>

Now time to test the auto-wiring. Create the main program and call the beans.

package springcore.autowiring;

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

public class PersonMain {

 ApplicationContext applicationContext;
 Person person;

 PersonMain() {
   applicationContext = new FileSystemXmlApplicationContext(
       "C:/config/autowiring-beans.xml");

   person = (Person) applicationContext.getBean("person");
 }

 public Address getAddress() {
   return person.getAddress();
 }

 public static void main(String[] args) {
   PersonMain personMain = new PersonMain();
   System.out.println("Person Address is: " + personMain.getAddress().getStreeName());
 }
}

AUTO-WIRING TYPES

  1. Auto-wiring by Name
    When we apply auto-wiring by name, spring container will look for the matching bean Name and inject it.
<bean id="person" class="springcore.autowiring.Person"
autowire="byName" />

  1. Auto-wiring by Type
    When we apply auto-wiring by type, spring container will look for the matching bean and inject it.
<bean id="person" class="springcore.autowiring.Person"
autowire="byType" />

  1. Auto-wiring by Constructor
    When we apply auto-wiring by a constructor, the spring container will look for the constructor of the bean and inject it.
<bean id="person" class="springcore.autowiring.Person"
autowire="constructor" />

Hope this post helps to understand the auto-wiring without annotation. I will cover the auto-wiring with annotation in another post.
Please share your comments and suggestions.
Happy Knowledge Sharing!!!

Thursday, August 6, 2015

Spring Dependency Injection / Inversion of Control (IoC)

INTRODUCTION

  • Spring Framework's very basic concept is Dependency Injection or IoC.
  • It is a reverse process of traditional java object initialization and instantiation.
  • When a java program starts running it, it will first create all the dependent object and then start executing even though those objects are used or not.
  • But Spring Framework's Dependency Injection will reverse the process of creating the object for any Class.
  • It will create the object whenever it is required at run-time.

SOFTWARES & TOOLS

  1. Eclipse IDE (Mars2) with Spring Plugins Installed.
  2. Java 1.6

USECASE

I would like to explain with a simple program which will read the data from different sources (flat file, db or from other sources)

IMPLEMENTATION

STEP 1

Create an interface for file reading.
package com.jbr.springcore.ioc;

public interface DataReader {
 String readData();
 String getSource();
}

STEP 2

Create an implementation for flat file reading.
package com.jbr.springcore.ioc;

import java.io.File;
import java.io.FileNotFoundException;

import java.util.Scanner;

public class FlatFileDataReader implements DataReader {
 private StringBuilder builder;
 private Scanner scanner;

 public FlatFileDataReader(String fileName) throws FileNotFoundException {
   scanner = new Scanner(new File(fileName));
   builder = new StringBuilder();
 }

 @Override
 public String readData() {
   while (scanner.hasNext()) {
     builder.append(scanner.next() + " ");
   }

   return builder.toString();
 }

 private String source;

 public void setSource(String source) {
   this.source = source;
 }

 public String getSource() {
   return source;
 }
}

STEP 3

Create service class.
package com.jbr.springcore.ioc;

public class DataReaderService {

 private DataReader dataReader;

 public DataReaderService(DataReader dataReader) {
   this.dataReader = dataReader;
 }

 public String readData() {
   return dataReader.readData();
 }

 public String getSource() {
   return dataReader.getSource();
 }
}

Now your bean is ready, configure it in bean configuration file.

STEP 4

Create Bean Configuration file & Properties file

  • This file will contain one or more bean configurations which will be used in your application
  • Changing the hardcoded values in the bean configuration file is a hectic job. So that I have created a property file with a list of properties and configured in bean configuration file.
<?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">

<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties"></property>
</bean>

<bean id="dataReaderService" class="com.jbr.springcore.ioc.DataReaderService">
<constructor-arg ref="flatFileReader" />
</bean>

<bean id="flatFileReader" class="com.jbr.springcore.ioc.FlatFileDataReader">
<constructor-arg value="${fileName}" />
<property name="source" value="${dataSource}"></property>
</bean>

</beans>

STEP 5

Create a properties file to have necessary property.
fileName=data.txt
dataSource=Data From Flat File

STEP 6

Create a client program to read the data from the file.
package com.jbr.springcore.ioc;

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

public class DataReaderMain {

 ApplicationContext applicationContext;
 DataReaderService dataReaderService;

 public DataReaderMain() {
   applicationContext = new ClassPathXmlApplicationContext("ioc-beans.xml");
   dataReaderService = (DataReaderService) applicationContext.getBean("dataReaderService");
 }

 public String getData() {
   return dataReaderService.readData();
 }

 public String getSource() {
   return dataReaderService.getSource();
 }

 public static void main(String[] args) throws Exception {
   DataReaderMain dataReaderMain = new DataReaderMain();
   System.out.println("Data Source: " + dataReaderMain.getSource());
   System.out.println("Data: " + dataReaderMain.getData());
 }
}

INPUT

Create a file and have some data like below.
This is a sample spring program

OUTPUT


Data Source: Data From Flat File
Data: This is a sample spring program

Further, if you want to read data from a database, write a separate class which will implement the DataReader Interface and configures it in the bean configuration.
Hope this post helps to understand the auto-wiring without annotation. I will cover the auto-wiring with annotation in another post.
Please share your comments and suggestions.
Happy Knowledge Sharing!!!

Wednesday, September 17, 2014

How to Resolve java.lang.NegativeArraySizeException in Spring


When will you get this Exception?

I was getting this exception when I start my spring application. I was searching the web for any better solution but nothing worked out.
Later I was able to remember the mistake which I have done.

What mistake I have done?
I have defined a property in my class and defined the same in the bean definition file as well. I have copy pasted my bean file into the tomcat.
Later I have removed my property from the java class file and I have not removed the property entries from the bean definition XML file.

What is the reason for the error?
We will get this exception when we have defined a property for a Class later which we have removed those properties from the class.

Solution
Remove the unwanted property definition from the bean definition file and redeploy bean definition XML files.