December 20 2014

Spring Setter Injection

Spring Setter Injection

The Spring IoC container also supports setter injection, which is the preferred method of dependency injection in Spring. Setter injection uses the set* methods in a class file to garner property names that are configurable in the spring XML config.

Example

Class Address contains three properties, getters and setters and print method to print all the properties. Class Entity contains three properties id(int),name(String) ,address(Address),getters and setter,print method to print all the values of Entity and Address. Continue reading

Category: Spring | LEAVE A COMMENT
December 20 2014

Constructor Injection In Spring

Constructor Injection In Spring

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with.Dependency Injection exists in two major variants

Constructor-based dependency injection

Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.

Continue reading

Category: Spring | LEAVE A COMMENT
December 18 2014

Spring @PostConstruct and @PreDestroy

Spring @PostConstruct and @PreDestroy

The @PostConstruct and @PreDestroy annotation are part of common-annotations.jar. Init and destroy can be implemented using xml and annotations.

Example

Class PostCnstrctPreDstryExample contains three properties(getters and setters),print() method to print the properties, intialize(@PostConstruct) and destroy(@PreDestroy) methods. The @PostConstruct method will be called after the message property set and @PreDestroy method will be called after the context.close();

Continue reading

Category: Spring | LEAVE A COMMENT
December 15 2014

Prime Numbers Program In Java

Prime Numbers Program In Java

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Algorithm

1.The maximum value should be taken from the user.

2.Iterating from 1 to the maximum value.

3.Checking whether value is divisible any number from 2 to maximum value-1

4.If it is divisible by any number then it is not a prime number.

5.If it is not divisible by any number then it is a prime number.

6.Print the results. Continue reading

December 13 2014

Palindrome String Program In Java

Palindrome String Program In Java

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.Some simple examples are RACECAR,DEED, LEVEL,PIP etc.
In the below program the value entered by the user is stored in String  enteredString. Iterating the enteredString  from last character to first character reverse of the entered string is constructed and stored in reverseString . If enteredString and reverseString are equal the entered is a palindrome. If enteredString and reverseString are not equal the given string is not a palindrome.

Program Continue reading

December 13 2014

Java LinkedHashSet

Java LinkedHashSet

Class LinkedHashSet extends HashSet. LinkedHashSet maintains a linked list of the elements in the set. LinkedHashSet maintains the order of the elements in which the elements were inserted. LinkedHashSet does not have any members of its own.

Constructors

LinkedHashSet( )
LinkedHashSet(Collection c)
LinkedHashSet(int capacity)
LinkedHashSet(int capacity, float fillRatio) Continue reading

December 13 2014

Java TreeSet Example

Java TreeSet Example

TreeSet provides an implementation of the Set interface. It uses tree for storage of Objects.Objects in TreeSet are stored in sorted Order. TreeSet is a good choice when objects need to be stored in sorted order.

Constructors

TreeSet()
TreeSet(Collection c)
TreeSet(Comparator c)
TreeSet(SortedSet s)

Continue reading

December 5 2014

Java Rethrow Exception

Java Rethrow Exception

throw is a special form of the return statement that only returns exception.Exception can be throwed by creating a new instance of it.When jvm encounters throw statement it stops execution and returns the exception to previous method in call stack.Re throwing is useful in case when programmer want to handle the exception and propagate the exception to other class.

Syntax :

try{

/ / Statements

}

catch(Exception e){

throw exception ;

}

Continue reading