In this post we will learn frequently asked Spring Interview Questions and answers for Experienced Developers. They are
1. What are the types of Dependency Injection Spring supports?
Setter Injection:
Ans: Setter-based DI is realized by calling setter methods on the user’s beans after invoking a no-argument constructor or no-argument static factory method to instantiate their bean.
Constructor Injection:
Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.
2.What bean scopes does Spring support? Explain them.
Ans: The Spring Framework supports following five scopes, three of which are available only if the users use a web-aware Application Context.
Singleton: This scopes the bean definition to a single instance per Spring IoC container.
Prototype: This scopes a single bean definition to have any number of object instances.
Request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext
Session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
Global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
3.What are the benefits of Spring Framework?
Ans:
4. Why Spring framework is needed?
Ans: Spring framework is needed because it is –
5.What are the methods of bean life cycle?
Ans: There are two important methods of Bean life cycle:
Setup – called when bean is loaded into container
Teardown – called when bean is unloaded into container
6. what is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code. The XmlBeanFactory is one of the implementations of Bean Factory which we have used in our project. The org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our XML file.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
Or
ClassPathResource resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);
7.What is the difference between singleton and prototype bean?
Ans: Basically, a bean has scopes which define 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.
8.What are the benefits of IoC.
Ans: It will minimize the amount of code in your application.
It will make your application easy to test because it doesn’t require any singletons or JNDI lookup mechanisms in your unit test cases.
It promotes loose coupling with minimal effort and least intrusive mechanism.
It supports eager instantiation and lazy loading of the services.
9.What do you understand by auto wiring?
Ans: The Spring container is able to autowire relationships between the collaborating beans. That is, it is possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of the BeanFactory.
10. What are the limitations with auto wiring?
Ans: Following are some of the limitations you might face with auto wiring:
Overriding possibility: You can always specify dependencies using <constructor-arg> and <property> settings which will override autowiring.
Primitive data type: Simple properties such as primitives, Strings and Classes can’t be autowired.
Confusing nature: Always prefer using explicit wiring because autowiring is less precise.
11.Describe Spring DAO support?
Ans: The Data Access Object (DAO) support in Spring makes it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch between the persistence technologies easily. It also allows you to code without worrying about catching exceptions that are specific to each of these technology
12.What are the different types of AutoProxying?
Ans: Following are the different types of AutoProxying:
BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator
Metadata autoproxying
13.How can beans be made singleton or prototype?
Ans: The bean tag has an attribute called ‘singleton’. The bean is singleton if its value is ‘TRUE’, otherwise the bean is a prototype.
14. What is ‘Throws advice’ in Spring?
Ans: ‘Throws Advice’ define the behavior when an exception occurs. It is an interface and it has no methods which need to be implemented.
A class that implements this interface should have method with this signature:
Void samplethrow (Throw table t)
Void samplethrow(Method m, Object[] o, Object target, Throw tablet)
15.How is Hibernate accessed using the Spring framework?
Ans: Hibernate can be accessed in the following two ways:
By IOC with a Callback and HibernateTemplate.
By applying an AOP Interceptor and broadening the HibernateDaoSupport.
16.Explain the advantages of using DAO module?
Ans: The database code can be kept clean and simple by using the DAO module. This helps in preventing problems that rise because of poor handling of closures of database resources. Also, the DAO module utilizes the AOP module to enable objects in the Spring application to use transaction management services.
1. What are the types of Dependency Injection Spring supports?
Setter Injection:
Ans: Setter-based DI is realized by calling setter methods on the user’s beans after invoking a no-argument constructor or no-argument static factory method to instantiate their bean.
Constructor Injection:
Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.
2.What bean scopes does Spring support? Explain them.
Ans: The Spring Framework supports following five scopes, three of which are available only if the users use a web-aware Application Context.
Singleton: This scopes the bean definition to a single instance per Spring IoC container.
Prototype: This scopes a single bean definition to have any number of object instances.
Request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext
Session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
Global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
3.What are the benefits of Spring Framework?
Ans:
- Extensive usage of Components
- Reusability
- Decoupling
- Reduces coding effort by using pattern implementations such as singleton, factory, service locator etc.
- Removal of leaking connections
- Declarative transaction management
- Easy to integrate with third party tools and technologies.
4. Why Spring framework is needed?
Ans: Spring framework is needed because it is –
- Very Light Weight Container
- Framework
- IOC
- AOP
5.What are the methods of bean life cycle?
Ans: There are two important methods of Bean life cycle:
Setup – called when bean is loaded into container
Teardown – called when bean is unloaded into container
6. what is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code. The XmlBeanFactory is one of the implementations of Bean Factory which we have used in our project. The org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our XML file.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
Or
ClassPathResource resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);
7.What is the difference between singleton and prototype bean?
Ans: Basically, a bean has scopes which define 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.
8.What are the benefits of IoC.
Ans: It will minimize the amount of code in your application.
It will make your application easy to test because it doesn’t require any singletons or JNDI lookup mechanisms in your unit test cases.
It promotes loose coupling with minimal effort and least intrusive mechanism.
It supports eager instantiation and lazy loading of the services.
9.What do you understand by auto wiring?
Ans: The Spring container is able to autowire relationships between the collaborating beans. That is, it is possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of the BeanFactory.
10. What are the limitations with auto wiring?
Ans: Following are some of the limitations you might face with auto wiring:
Overriding possibility: You can always specify dependencies using <constructor-arg> and <property> settings which will override autowiring.
Primitive data type: Simple properties such as primitives, Strings and Classes can’t be autowired.
Confusing nature: Always prefer using explicit wiring because autowiring is less precise.
11.Describe Spring DAO support?
Ans: The Data Access Object (DAO) support in Spring makes it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch between the persistence technologies easily. It also allows you to code without worrying about catching exceptions that are specific to each of these technology
12.What are the different types of AutoProxying?
Ans: Following are the different types of AutoProxying:
BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator
Metadata autoproxying
13.How can beans be made singleton or prototype?
Ans: The bean tag has an attribute called ‘singleton’. The bean is singleton if its value is ‘TRUE’, otherwise the bean is a prototype.
14. What is ‘Throws advice’ in Spring?
Ans: ‘Throws Advice’ define the behavior when an exception occurs. It is an interface and it has no methods which need to be implemented.
A class that implements this interface should have method with this signature:
Void samplethrow (Throw table t)
Void samplethrow(Method m, Object[] o, Object target, Throw tablet)
15.How is Hibernate accessed using the Spring framework?
Ans: Hibernate can be accessed in the following two ways:
By IOC with a Callback and HibernateTemplate.
By applying an AOP Interceptor and broadening the HibernateDaoSupport.
16.Explain the advantages of using DAO module?
Ans: The database code can be kept clean and simple by using the DAO module. This helps in preventing problems that rise because of poor handling of closures of database resources. Also, the DAO module utilizes the AOP module to enable objects in the Spring application to use transaction management services.
No comments:
Post a Comment