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.
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
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.
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.
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();
Factorial program in java using recursion
Factorial is the product of all positive integers less than or equal to n.
1.Take the input from the user
2.Pass the number to the method
3.Calculate the product of the number and pass number-1 to the same method
4.Repeat the step 3 till number becomes 1
5.Print the result
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.
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
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
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.
LinkedHashSet(int capacity, float fillRatio) Continue reading
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.
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.
/ / Statements
throw exception ;
ListIterator is an interface that contains methods to retrieve the elements from collection from both forward and backward direction. Continue reading