May 28 2015

instanceof operator in java

instanceof operator in java

The instanceof operator compares an object to a specific type. It returns true if the object is an instance of the named class or class’s super classes otherwise it returns false. The instanceof operator can also be used for interfaces, it returns true if object implements interface otherwise false.

The left operand should be the object and right operand should be the class or interface.

“answersz.com” instanceof String

You cannot use the instanceof operator to test different class. It will give you compilation error.

Example Continue reading

May 27 2015

Hibernate cascade types

Hibernate cascade types

Cascading means that if you do any operation on an object it acts on the related objects as well. If you insert, update or delete an object the related objects (mapped objects) are inserted, updated or deleted.

Hibernate does not navigate to object associations when you are performing an operation on the object. If you do not use cascade, you have to do the operations like save, update or delete on each and every object explicitly.

To enable cascading for related association, the cascade attribute has to be configured for association in mapping metadata

Hibernate cascade types Continue reading

May 27 2015

Singleton design pattern in java

Singleton design pattern in java

Singleton is a design pattern that restricts the instantiation of a class to one object and provides a global point of access to it.

The singleton pattern is implemented in java by creating a class with a method that returns reference to the object.

Singleton class can be implemented following

  • Declaring constructor as private to restrict creation of instance in other classes
  • Creating private static object reference of the class and this is the only instance of the class
  • Creating static method that returns the static object reference of the class. This method has to be called to get the instance of the class

Eager initialization

The single instance is created at the time of class loading Continue reading

May 25 2015

Jsf session timeout

Jsf session timeout

Session timeout is a property that is assigned to the Session object. If user do not request or refresh the page with the specified time, the session will end.

Jsf session timeout can be configured in two ways

  1. Adding ‘session-timeout’ in web.xml

Configure the value for ‘session-timeout’ of ‘session-config’ element in web.xml .The value should be specified in minutes

In the below example, if user does not request or refresh for 30 minutes the container will end the session. Continue reading

May 25 2015

Java override toString

Java override toString

toString method is part of Object class. When you pass an object reference to the System.out.println() method, the object’s toString() method is called.

The toString method for Object class returns string with class name followed by @ symbol, followed by unsigned hexadecimal representation of the object’s hash code.

Class Name+ ‘@’ + hash Code

You have to override toString method if you want to see something meaningful about object of your class.

public String toString()

Example: Without toString method Continue reading

May 20 2015

Spring IoC Container and Beans

Spring IoC Container and Beans

Spring IoC Container

Spring IoC Container is the basis for spring framework. The primary job of spring IoC container is to instantiate the Java object, injecting its dependencies and managing them.

Spring Bean

The object created and managed by spring core container is called a Spring Bean. The bean objects has to be added in the configuration metadata which is taken by the Spring IoC Controller.

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework IoC container.

BeanFactory Continue reading

Category: Spring | LEAVE A COMMENT
May 20 2015

Spring abstract bean

Spring abstract bean

If you want a bean definition that is a template (parent) for other beans and should not be instantiated, it can be marked as an abstract bean. You can make a spring bean as abstract by configuring ‘abstract’ attribute of the ‘<bean> ‘  tag to ‘true’.

The abstract bean definition will not be instantiated by the spring container.

Example

In the below example, ‘employeeService‘  bean is the template (parent) for employeeService1 and employeeService1 beans. ‘employeeService’ is marked as abstract and it will not be instantiated by the spring container. Continue reading

Category: Spring | LEAVE A COMMENT
May 20 2015

Spring prototype component

Spring prototype component

A prototype bean is instantiated every time when a request is made for the bean. Spring IoC container creates one instance and returns the new object reference for each request.

The default scope of the bean is singleton. If you want to configure the bean with prototype scope, you have to update the scope attribute.

Xml   Continue reading

Category: Spring | LEAVE A COMMENT
May 19 2015

Spring IoC Interview Questions

Spring IoC Interview Questions

 What is Inversion of Control?

Inversion of Control (IoC) describes that a dependency injection need to be done by an external entity instead of creating the dependencies by the component itself. All the dependencies and relationships are created by IoC container. IoC is also known as dependency injection (DI).

What is Spring IoC container?

Spring IoC container is the basis for spring framework. The primary job of spring IoC container is to instantiate the java object, injecting its dependencies and managing them.

The object created and managed by spring core container is called a Spring Bean. The bean objects has to be added in the configuration metadata which is taken by the Spring IoC Controller. read more

What are the different Spring IoC containers? Continue reading