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.
What is the difference between singleton and prototype bean?
Mainly it is the scope of a beans which defines their existence on the application
Singleton : It means single bean definition to a single object instance per Spring IOC container.
Prototype : It means a single bean definition to any number of object instances.
How do beans become 'singleton' or prototype?
- There exists an attribute in bean tag, called 'singleton’.
- If it is marked 'true', the bean becomes 'singleton'.
- If it is marked 'false', the bean becomes 'prototype'.
What type of transaction Management Spring support?
Spring supports two types of transaction management:
1. Programmatic transaction management
2. Declarative transaction management.
When do you use programmatic and declarative transaction management?
Programmatic transaction management is used preferably when you have a small number of transactional operations.
Incase of large number of transactional operations it is better to use declarative transaction management.
What is IOC?
- IOC stands for Inversion of Control pattern.
- It is also called as dependency injection.
- This concept says that you do not create your objects but describe how they should be created.
- Similarly, you do not directly connect your components and services together in code but describe which services are needed by which components in a configuration file.
- A container then hooks them all up.
What are the different types of IoC (dependency injection)?
- There are three types of dependency injection:
1. Constructor Injection : Here dependencies are provided as constructor parameters.
2. Setter Injection : Dependencies are assigned through JavaBeans properties.
3. Interface Injection : Injection is performed through an interface.
- Spring supports only first two categories of Injection.
What are the benefits of IOC?
The main benefits of IOC or dependency injections are :
1. It minimizes the amount of code in your application.
2. It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
3. Loose coupling is promoted with minimal effort and least intrusive mechanism.
4. IOC containers support eager instantiation and lazy loading of services.
What is Bean Wiring?
- Bean wiring means creating associations between application components i.e. beans within the spring container.
How do you access Hibernate using Spring ?
There are two ways to Spring’s Hibernate integration:
1. By Inversion of Control with a HibernateTemplate and Callback.
2. By extending HibernateDaoSupport and Applying an AOP Interceptor.
How would you integrate Spring and Hibernate using HibernateDaoSupport?
This can be done through Spring’s SessionFactory called LocalSessionFactory. The steps in integration process are:
1. Configure the Hibernate SessionFactory.
2. Extend your DAO Implementation from HibernateDaoSupport.
3. Wire in Transaction Support with AOP.
What are the various transaction manager implementations in Spring?
1. DataSourceTransactionManager : PlatformTransactionManager implementation for single JDBC data sources.
2. HibernateTransactionManager: PlatformTransactionManager implementation for single Hibernate session factories.
3. JdoTransactionManager : PlatformTransactionManager implementation for single JDO persistence manager factories.
4. JtaTransactionManager : PlatformTransactionManager implementation for JTA, i.e. J2EE container transactions.
What are the different modules in spring framework?
The Spring features or organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation and Test, as depicted below.
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.
Symantec Spring MVC Recently Asked Interview Questions Answers |
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.
What is the difference between singleton and prototype bean?
Mainly it is the scope of a beans which defines their existence on the application
Singleton : It means single bean definition to a single object instance per Spring IOC container.
Prototype : It means a single bean definition to any number of object instances.
How do beans become 'singleton' or prototype?
- There exists an attribute in bean tag, called 'singleton’.
- If it is marked 'true', the bean becomes 'singleton'.
- If it is marked 'false', the bean becomes 'prototype'.
What type of transaction Management Spring support?
Spring supports two types of transaction management:
1. Programmatic transaction management
2. Declarative transaction management.
When do you use programmatic and declarative transaction management?
Programmatic transaction management is used preferably when you have a small number of transactional operations.
Incase of large number of transactional operations it is better to use declarative transaction management.
What is IOC?
- IOC stands for Inversion of Control pattern.
- It is also called as dependency injection.
- This concept says that you do not create your objects but describe how they should be created.
- Similarly, you do not directly connect your components and services together in code but describe which services are needed by which components in a configuration file.
- A container then hooks them all up.
What are the different types of IoC (dependency injection)?
- There are three types of dependency injection:
1. Constructor Injection : Here dependencies are provided as constructor parameters.
2. Setter Injection : Dependencies are assigned through JavaBeans properties.
3. Interface Injection : Injection is performed through an interface.
- Spring supports only first two categories of Injection.
What are the benefits of IOC?
The main benefits of IOC or dependency injections are :
1. It minimizes the amount of code in your application.
2. It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
3. Loose coupling is promoted with minimal effort and least intrusive mechanism.
4. IOC containers support eager instantiation and lazy loading of services.
What is Bean Wiring?
- Bean wiring means creating associations between application components i.e. beans within the spring container.
How do you access Hibernate using Spring ?
There are two ways to Spring’s Hibernate integration:
1. By Inversion of Control with a HibernateTemplate and Callback.
2. By extending HibernateDaoSupport and Applying an AOP Interceptor.
How would you integrate Spring and Hibernate using HibernateDaoSupport?
This can be done through Spring’s SessionFactory called LocalSessionFactory. The steps in integration process are:
1. Configure the Hibernate SessionFactory.
2. Extend your DAO Implementation from HibernateDaoSupport.
3. Wire in Transaction Support with AOP.
What are the various transaction manager implementations in Spring?
1. DataSourceTransactionManager : PlatformTransactionManager implementation for single JDBC data sources.
2. HibernateTransactionManager: PlatformTransactionManager implementation for single Hibernate session factories.
3. JdoTransactionManager : PlatformTransactionManager implementation for single JDO persistence manager factories.
4. JtaTransactionManager : PlatformTransactionManager implementation for JTA, i.e. J2EE container transactions.
What are the different modules in spring framework?
The Spring features or organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation and Test, as depicted below.
Post a Comment