Spring Interview Questions | Page 2


Question 11:

What are the various bean scope in Spring?

The Spring Framework supports following five bean scope:

 

Scope Description
 singleton  The scope of the bean definition is single instance per Spring IoC container (default).
 prototype  The scope of a single bean definition is to have any number of object instances.
 request  The scope of a bean definition is per HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
 session  The scope of a bean definition is per HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
 global-session  The scope of a bean definition is per global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
Question 12:

Define singleton bean scope in Spring with example.

The scope of a bean defined as singleton is single instance per Spring IoC container (default). Hence, the Spring IoC container creates exactly one instance of the object defined by that bean definition. All subsequent requests and references for that named bean return the same bean object.
Singleton is the default scope.

For example:-

SPRING-CONFIG.XML :-

<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-2.5.xsd">

                <bean id="demoBean" 
                 class="com.modernpathshala.DemoBean" />

</beans>

MyDemoApplication :-

public class MyDemoApplication 
{
   public static void main( String[] args )
   {
       ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-config.xml"});
       DemoBean demoOne = (DemoBean)context.getBean("demoBean");
       demoOne.setStatus("I have singleton scope.");
       System.out.println("Output : " + demoOne.getStatus());

       DemoBean demoTwo = (DemoBean)context.getBean("demoBean");
       System.out.println("Output : " + demoTwo.getStatus());
	}
}

OUTPUT :

Output : I have singleton scope.

Output : I have singleton scope.
Question 13:

Define prototype bean scope in Spring with example.

The scope of bean defined as prototype, every time a request for that specific bean is made a new bean will be instantiated by Spring Ioc container.

For example:-

spring-config.xml :-

<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-2.5.xsd">

                 <bean id="demoBean" 
                  class="com.modernpathshala.DemoBean" scope="prototype" />

</beans>

MyDemoApplication :-

 public class MyDemoApplication 
{
     public static void main( String[] args )
     { 
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-config.xml"});
        DemoBean demoOne = (DemoBean)context.getBean("demoBean");
        demoOne.setStatus("I have prototype scope.");
        System.out.println("Output : " + demoOne.getStatus());
        DemoBean demoTwo = (DemoBean)context.getBean("demoBean");
        System.out.println("Output : " + demoTwo.getStatus());
     }
}

Output:-

Output : I have prototype scope.
Output : null
Question 14:

What do you mean by JointPoint?

A join point is some specific point in the application such as method execution, exception handling, changing object variable values etc. In Spring AOP, a join point is always the execution of a method.

Question 15:

What is an advice?

Advice is the action taken by an action at a particular join point. In other words this is an actual piece of code that is invokedwhen a certain join point with matching pointcut is reached in the application.
For example :- Implementation for logging/Log4j.


Question 16:

Explain different types of advice.

Various types of advice along with description is listed below:

Advice Description
Before advice Advice that executes before method execution.
After returning advice Advice that executes after method, execution completes normally.
After throwing advice Advice that executes if a method exits by throwing an exception.
After (finally) advice Advice that executes after method execution completes irrespective of exception occurred or normal exit.
Around advice Advice that surrounds method invocation. Around advice can perform custom behaviour before and after invocation.
Question 17:

What are the types of the transaction management Spring supports?

Spring Framework supports:

  • Programmatic transaction management.
  • Declarative transaction management.
Question 18:

What are the benefits of using Dependency Injection and IoC Containers?

There are several benefits from using dependency injection containers. Some of these benefits are:

  • Reduced Dependencies
  • Reduced Dependency Carrying
  • More Reusable Code
  • More Testable Code
  • More Readable Code
Question 19:

What is Circular Dependency scenario in Spring?

If you use predominantly constructor injection, it is possible to create an unresolvable circular dependency scenario.

For example, Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection.If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference at runtime, and throws a BeanCurrentlyInCreationException.

Question 20:

What's the difference between @Component, @Controller, @Repository & @Service annotations in Spring?

@Component: It is a generic stereotype for any Spring-managed component.

@Controller: @Controller annotated class indicates that it is a controller components, and mainly used at presentation layer.

@Service: It indicates annotated class is a Service component in the business layer.

@Repository: @Repository annotation is used with in the persistence layer, this acts like database repository.

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools.

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media