Monday, March 23, 2015

Spring IOC Interview Questions (Experience

  1. What would happen if we have a prototype bean injected into a singleton bean ? How many objects of prototype bean object will be created ? 
    When a singleton bean is created , a single instance of the prototype bean objecte is created. It won't create a new prototype bean.
  2. Are singleton beans in Spring a regular singleton object ?
  3. Are Singleton beans thread safe ?
  4. A bean can be marked abstract by abstract=true, does not that mean we have to make the corresponding java class abstract ? 
    No, a bean marked abstract makes the bean not instanciatable, also it makes an ideal situation to use this reference as parent to other child bean definition. Making the corresponding java as abstract is not necessary but can be done
  5. If an inner bean is defined with an id, can you use this id to fetch the bean from the container ?
    No, An bean defined inner bean can't be accessed even if the id attribute has value. so getBean("theInnerId") will fail with NoSuchBeanDefinitionException.
  6. What is the implementation of List is used when you use the tag in a bean definition ?
  7. How do you use a particular implementation of collection in your bean definition ? 
    You can use the and with set-class to the implementation you want to use.For example to use linkedList as implementation, and don't forget to include the schema details in the beans tag. Also util tag can let you create id of the collection , thus this can be refered or shared with any other beans by using the regular way , ie the ref tag.

Saturday, March 21, 2015

How to create immutable class in java??

How to create Immutable class?

An immutable class is one whose state can not be changed once created. There are certain guidelines to create an class immutable. In this post, we will revisit these guidelines.

Example to create Immutable class

In this example, we have created a final class named Employee. It have one final datamember, a parameterized constructor and getter method.
  1. public final class Employee{  
  2. final String pancardNumber;  
  3.   
  4. public Employee(String pancardNumber){  
  5. this.pancardNumber=pancardNumber;  
  6. }  
  7.   
  8. public String getPancardNumber(){  
  9. return pancardNumber;  
  10. }  
  11.   
  12. }  

The above class is immutable because:
  • The instance variable of the class is final i.e. we cannot change the value of it after creating an object.
  • The class is final so we cannot create the subclass.
  • There is no setter methods i.e. we have no option to change the value of the instance variable.
These points makes this class as immutable.