August 30, 2018

Sreekanth B

CA Technologies Spring MVC Recently Asked Interview Questions Answers

What is Aspect Oriented Programming (AOP)?

- Basically Aspect oriented programming complements object oriented programming by providing another way of programming model structure.
- In addition to classes, AOP gives you aspect, which enables modularization of concerns such as Transaction management or logging and can be separated out from the application business logic of the code (these kinds of concerns are termed as crosscutting concerns). AOP supports separating application business logic from System services.

What is IOC or Dependency Injection?

The basic concept of IOC (Dependency of Injection) is that you do not create your objects but describe how they should be created.

You don’t directly connect your component and services together in code but describe which services are needed by which component in configuration file.

You just need to describe the dependency, the Spring container is then responsible for hooking it all up.
CA Technologies Spring MVC Recently Asked Interview Questions Answers
CA Technologies Spring MVC Recently Asked Interview Questions Answers

When to use Dependency Injections?

There are different scenarios where you Dependency Injections are useful.

- You need to inject configuration data into one or more component.
- You need to inject the same dependency into multiple components.
- You need to inject different implementation of the same dependency.
- You need to inject the same implementation in different configuration.
- You need some of the services provided by container.

When you should not use Dependency Injection?

There were scenarios where you don’t need dependency injections e.g.
- You will never need a different implementation.
- You will never need different configurations.

What is Bean Factory in Spring?

- A Bean Factory is like a factory class that contains collections of beans. The Bean Factory holds bean definition of multiple beans within itself and then instantiates the bean when asked by client.
- Bean Factory is actual representation of the Spring IOC container that is responsible for containing and managing the configured beans.

Different Spring Bean Scope.

1. singleton : Return a single bean instance per Spring IOC container.
2. prototype : Return a new bean instance each time when requested.
3. request : Return a single bean instance per HTTP request.
4. session : Return a single bean instance per HTTP session.
5. global session : Return a single bean instance per global HTTP session and only valid when used in portlet context.

How you will decide when to use prototype scope and when singleton scope bean?

- You should use the prototype scope for all beans that are stateful and the singleton scope should be used for stateless beans.

What are the different types of IOC?

There are three types of dependency Injection:

1. Constructor Injection :

- Dependencies are provided as a constructor parameter.

- Example : You want to inject some object say 'Foo' through constructor in class 'HellowWorld' like below.
public class HelloWorld
{
public HelloWorld(Foo foo)
   {
       this.foo = foo;
   }
}

- In configuration file you need to do following entry.
<bean id="helloWorldBean" class="com.xyz.services.HelloWorld">
<constructor-arg ref="fooBean" />
</bean>
<bean id="fooBean" class="com.xyz.service.Foo">
</bean>

2. Setter Injection :

- Dependencies are assigned through setter method.

- Example : Same example as above.
public class HelloWorld
{
private Foo fooBean;
public HelloWorld(){ }
public void setFooBean(Foo fooBean)
{
   this.fooBean=fooBean;
}

- And in configuration file you need to do following entry.
<bean id="helloWorldBean" class=" com.xyz.services.HelloWorld">
<property name=fooBean ref="fooBean" />
</bean>
<bean id="fooBean" class="com.xyz.service.Foo">
</bean>

3. Interface Injection :

Injection is done through an interface and not supported in spring framework.
How to Call Stored procedure in Spring Framework?

- To call a Stored procedure in Spring framework you need to create Class which will should extends StoredProcedure class.
- Take the example of getting Employee Details by Employee Id. package com.mytest.spring.storeproc
import java.sql.ResultSet;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
public class EmployeeInfo extends StoredProcedure
{
   private static final String EMP_ID = "EMP_ID";
   private static final String EMP_NAME = "EMP_NAME";
   private static final String JOIN_DATE = "JOIN_DATE";
   public SnapshotSearchStoredProcedure(DataSource dataSource, String procedureName)
   {
       super(dataSource, procedureName);
       declareParameter(new SqlParameter(EMP_ID, Types.NUMERIC));
       declareParameter(new SqlOutParameter(EMP_NAME, Types.VARCHAR));
       declareParameter(new SqlOutParameter(JOIN_DATE, Types.VARCHAR));
       compile ();
   }
   public Map execute(Integer empId)
   {
       Map<String, Object> inputs = new HashMap<String, Object>();
       inputs.put(P_CLD_IDR, empId);
       Map<String, Object> result = execute (inputs);
       return result;
   }
}
- You just need to call the execute method from the DAO layer.

Differentiate between BeanFactory and ApplicationContext in spring.

With ApplicationContext more than one config files are possible while only one config file or .xml file is possible with BeanFactory.

ApplicationContext publishes events to beans that are registered as listeners while BeanFactory doesn't support this

ApplicationContext support internationalization messages, application life-cycle events, validation and many enterprise services like JNDI access, EJB integration, remoting etc. while BeanFactory doesn't support any of these.

Explain the dependency resolution process.

The dependency resolution process is carried out as follows:

1. Creation an initialization of application text inside configuration metadata( can be specified in XML, java code,annotations) is done. This describes all the beans.

2. For each bean, its dependency is expressed in the form of properties, constructor arguments, or
arguments to the static-factory method if that is used instead of a normal constructor.

3. Each property or constructor argument is actual definition of the value which belong to set, or a reference to another bean which is in the container.

4. Each property or constructor argument which is a value is gone through conversion from its specified format to the actual type of that property or constructor argument.

How can you use spring to resolve collaborators for your bean? Also state the advantages of auto wiring?

The spring can be used to resolve collaborators automatically by inspecting contents of the ApplicationContext.

The Autowiring has the following advantages in spring:

1. Reduces requirement for specifying properties or construction arguments.

2. Updates configuration while evolving of objects. To understand this lets consider an example: modifying would not be required in configuration of a dependency while adding it to a class.

3. Switching to explicit wiring when code base is stable can be neglected.

List the limitations of auto wiring.

Autowiring has the following limitations :

1. Overriding and auto wiring are caused due to dependency in property and constructor argument setting.

2. Less precise as compared to explicit wiring.

3. Tools that generate documentation using a spring might not have access to wiring information.

4. There is a possibility of clash between bean definitions and argument or method to be wired.

5. The problem does not occur much in case of maps, arrays and collection and cannot be resolved randomly for dependencies which expect one value.

In scenarios where you have to avoid using auto wiring, what are the other options that can be explored to achieve the same?

In scenarios where using autowiring is prohibited, the following replacements can achive the same :

1. Abandon autowiring for favor of explicit wiring.

2. Avoid autowiring for a bean definition by setting the autowire-candidate for attributes to false as
described in the next section.

3. Set a single bean definition as the primary candidate by setting the primary attribute of its <bean/> element to true.

4. When Java 5 or later is used, implementation needs more fine-grained control available with annotation-based configuration.

How do you call the multiple life cycle mechanisms configured for the same bean?

Multiple lifecycle mechanisms which are configured for the same bean, with different initialization methods, are called by the following ways :

1. Methods annotated with use of @PostConstruct.

2. afterPropertiesSet() as stated by the InitializingBean callback interface.

3. Custom configuration init() method

Destroy methods are called in the same order as follows :

1. Methods annotated with the use of @PreDestroy.

2. destroy() as stated by the DisposableBean callback interface.

3. destroy() method with custom configuration.

What are the methods associated with the FactoryBean interface?

Following are the methods associated with factory bean interface:

1. Object getObject() : This returns an instance of the object which the factory creates. This instance can possibly be shared, depending on the factory returns singletons or prototypes.

2. Boolean isSingleton() : This returns true if this FactoryBean returns singletons, otherwise false.

3. Class getObjectType() : This returns the object type returned by the getObject() method or null if the type is unknown in advance.

What are the functionalities provided by the context package to enhance the BeanFactory functionality?

The functionalities provided by context package are as follows:

1. Message access in i18n-style, through the MessageSource interface.

2. Resource access such as URLs and files, through which, the ResourceLoader interface, can be accessed and used.

3. Publication of events, through the beans that allow, implementing the ApplicationListener interface. It is done through the use, of the ApplicationEventPublisher interface.

4. Multiple loading contexts, allowing each to be focused on one particular layer, such as the web layer of an application, through the HierarchicalBeanFactory interface.

What is spring framework? Why Java programmer should use Spring framework?

Very common Spring interview question, Spring is a framework which helps Java programmer in development. Spring provides dependency Injection and IOC container, Spring MVC flow and several useful API for Java programmer.

What is default scope of bean in Spring framework ?

The default scope of a Spring bean is Singleton scope.


Does Spring singleton beans are thread-safe ?

No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe.


Subscribe to get more Posts :