Pages

Spring Interview Questions Answers - Mostly Asked 2

Spring Interview Questions - 2

Question: What are the pros or benefits of Spring framework ?

The pros of Spring framework are as follows:
  • Spring is an open source framework and free to download.
  • Spring has layered architecture. You can select the feature you wants, you can have Struts MVC and Springs IOC container in one application itself. Eventhough spring has MVC framework if you want you can opt out.
  • Spring Enables Plain Old Java Object (POJO) Programming. POJO programming enables continuous integration and testability.
  • Dependency Injection is really cool stuff, spring 3.0 onwards the introduction of component-scan/autowiring and Spring Expression Language makes it even spicier.
  • spring is lightweight.

Question: What is Dependency Injection/Inversion Of Control(IOC) in Spring framework ?

The basic concept of the Dependency Injection or Inversion of Control is that, programmer do not need to create the objects, instead just describe how it should be created. No need to directly connect your components and services together in program, instead just describe which services are needed by which components in a configuration file/xml file. The Spring IOC container is then responsible for binding it all up.
In other words, while applying Inversion Of Control, at the time of object creation, objects are given their dependencies by some external entity that coordinates each object in the system. That means, dependencies are injected into objects at the time of their creation. So, Inversion of Control means an inversion of responsibility with regard to how an object obtains references to collaborating objects.

Question: What are the different types of Inversion of Control or dependency injection ?

There are three different types of Inversion of Control or dependency injection:
  • Setter Injection: Dependencies are injected through JavaBeans properties (ex: setter/Getter methods in bean objects).
  • Constructor Injection: Dependencies are assigned as constructor parameters.
  • Interface Injection: Injection is done through an interface.

Constructor and Setter Injection are the two dependency injection method which Spring supports.

Question: What are the advantages or Pros of IOC (Dependency Injection) ?

Advantages of Dependency Injection/Inversion of Control are as follows:
  • Dependency Injection minimizes the amount of code in any application. Dependency is handled by the framework itself.
  • Dependency Injection makes developers life easier. With Inversion of Control containers developers do not need to think about how services are created and how to get references to the ones he needs.
  • Easily scalable applications. It’s very easy to add additional services by adding a new constructor or a getter/setter method with a minimal configuration. With Spring Framework 3.0, its even easier as <context:component-scan base-package=”com.blah.blah”/> will do everything for you, you don’t need to add getter and setter method and beans for each dependency injection, just autowire the services wherever it needed.<Read how spring 3.0 made a developers life easier>
  • Dependency Injection makes your application more test-friendly by not demanding any JNDI lookup mechanisms or singletons in your test cases. IOC containers make testing and switching implementations easy by allowing you to inject your own objects into the object under test.
  • Comparing to other options like factory design pattern the IOC container is injecting the dependency into requesting piece of code where as the factory design pattern is more intrusive and components or services need to be requested explicitly.
  • IOC containers support eager instantiation and lazy loading of services.
  • IOC Containers provide support for instantiation of cyclical dependencies, managed objects, life cycles management and dependency resolution between managed objects etc.

Question: What are the difference between BeanFactory and ApplicationContext in spring?


ApplicationContext.

BeanFactory
Here we can have more than one config files possible
In this only one config file or .xml file
Application contexts can publish events to beans that are registered as listeners
Doesn’t support.
Support internationalization (I18N) messages
It’s not
Support application life-cycle events, and validation.
Doesn’t support.
Support  many enterprise services such JNDI access, EJB integration, remoting
Doesn’t support.


Question: What is difference between singleton and prototype bean?

Ans: Basically a bean has scopes which defines their existence on the application

Singleton: means single bean definition to a single object instance per Spring IOC container.
Prototype: means a single bean definition to any number of object instances.
Whatever beans we defined in spring framework are singleton beans. There is an attribute in bean tag named ‘singleton’ if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true. So, all the beans in spring framework are by default singleton beans.
<bean id="createNewStock" class="springexample.stockMarket.CreateNewStockAccont" singleton=”false”
     <property name="newBid"/>
</bean>

Question: What is bean wiring?

Ans: Combining together beans within the Spring container is known as bean wiring or wiring. When wiring beans, you should tell the container what beans are needed and how the container should use dependency injection to tie them together.


Question: What are the important beans lifecycle methods?

Ans: There are two important bean lifecycle methods. The first one is setup which is called when the bean is loaded in to the container. The second method is the teardown method which is called when the bean is unloaded from the container.

Question: Explain Bean-LifeCycle.

Ans: Spring framework is based on IOC so we call it as IOC container also. So Spring beans reside inside the IOCcontainer. Spring beans are nothing but Plain old java object (POJO).

Following steps explain their life cycle inside container.
  • Container will look the bean definition inside configuration file (e.g. bean.xml).
  • Using the dependency injection, spring populates all of the properties as specified in the bean definition.
  • If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
  • If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
  • If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization()methods will be called before the properties for the Bean are set.
  • If an init() method is specified for the bean, it will be called.
  • If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
  • If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.

Question: How can you override beans default lifecycle methods?

Ans:The bean tag has two more important attributes with which you can define your own custom initialization and destroy methods. Here I have shown a small demonstration. Two new methods fooSetup and fooTeardown are to be added to your Foo class.
<beans>
  <bean id="bar" class="com.act.Foo"
     init-method="fooSetup" destroy="fooTeardown"/>
</beans>

Question: What are Inner Beans?

When wiring beans, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The drawback of this bean is that it cannot be reused anywhere else.

Question: What is Auto wiring?

You can wire the beans as you wish. But spring framework also does this work for you. It can auto wire the related beans together. All you have to do is just set the autowire attribute of bean tag to an autowire type.



<beans>
     <bean id="bar" class="com.act.Foo" Autowire="autowire type"/>
</beans>

Question: How do add a bean in spring application?








<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
<beans>
   <bean id="foo" class="com.act.Foo"/>
        <bean id="bar" class="com.act.Bar"/
</beans>
In the bean tag the id attribute specifies the bean name and the class attribute specifies the fully qualified class name.

Question: What are different types of Autowire types?

There are four different types by which autowiring can be done.
  • byName
  • byType
  • constructor
  • autodetect

Question: What is an Aspect?

An aspect is the cross-cutting functionality that you are implementing. It is the aspect of your application you are modularizing. An example of an aspect is logging. Logging is something that is required throughout an application. However, because applications tend to be broken down into layers based on functionality, reusing a logging module through inheritance does not make sense. However, you can create a logging aspect and apply it throughout your application using AOP.

Question: What is a Jointpoint?

A joinpoint is a point in the execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.

Question: What is an Advice?

Advice is the implementation of an aspect. It is something like telling your application of a new behavior. Generally, and advice is inserted into an application at joinpoints.

Question:  What is a Pointcut?

A pointcut is something that defines at what joinpoints an advice should be applied. Advices can be applied at any joinpoint that is supported by the AOP framework. These Pointcuts allow you to specify where the advice can be applied.

Question: What is an Introduction in AOP?

An introduction allows the user to add new methods or attributes to an existing class. This can then be introduced to an existing class without having to change the structure of the class, but give them the new behavior and state.


Question: What is a Target?

A target is the class that is being advised. The class can be a third party class or your own class to which you want to add your own custom behavior. By using the concepts of AOP, the target class is free to center on its major concern, unaware to any advice that is being applied.


Question: What is a Proxy?

A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.

Question: What is meant by Weaving?

The process of applying aspects to a target object to create a new proxy object is called as Weaving. The aspects are woven into the target object at the specified joinpoints.


Question: What are the different points where weaving can be applied?

  • Compile Time
  • Classload Time
  • Runtime

Question: What are the different advice types in spring?

  • Around : Intercepts the calls to the target method
  • Before : This is called before the target method is invoked
  • After : This is called after the target method is returned
  • Throws : This is called when the target method throws and exception
  • Around : org.aopalliance.intercept.MethodInterceptor
  • Before : org.springframework.aop.BeforeAdvice
  • After : org.springframework.aop.AfterReturningAdvice
  • Throws : org.springframework.aop.ThrowsAdvice

 

Question: Explain about PreparedStatementCreator?

Ans: PreparedStatementCreator is one of the most common used interfaces for writing data to database. The interface has one method createPreparedStatement().
1
2
PreparedStatement <strong>createPreparedStatement</strong>
(Connection conn) throws SQLException;
When this interface is implemented, we should create and return a PreparedStatement from the Connection argument, and the exception handling is automatically taken care off. When this interface is implemented, another interface SqlProvider is also implemented which has a method called getSql()which is used to provide sql strings to JdbcTemplate.

No comments:

Post a Comment

Your Comment and Question will help to make this blog better...