Spring Beans interview question

Posted by: | Published: Sunday, March 20, 2016 | Categories:
1. What are Spring beans? 
The Spring Beans are Java Objects that form the backbone of a Spring application. They are instantiated, assembled, and managed by the Spring IoC container. These beans are created with the configuration metadata that is supplied to the container, for example, in the form of XML definitions. Beans 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. 


2. What does a Spring Bean definition contain? 
A Spring Bean definition contains all configuration metadata which is needed for the container to know how to create a bean, its lifecycle details and its dependencies. 

3. How do you provide configuration metadata to the Spring Container? 
There are three important methods to provide configuration metadata to the Spring Container: XML based configuration file. Annotation-based configuration Java-based configuration 

4. How do you define the scope of a bean? 
When defining a in Spring, we can also declare a scope for the bean. It can be defined through the scope attribute in the bean definition. For example, when Spring has to produce a new bean instance each time one is needed, the bean’s scope attribute to be prototype. On the other hand, when the same instance of a bean must be returned by Spring every time it is needed, the the bean scope attribute must be set to singleton. 

5. Explain the bean scopes supported by Spring 
 There are five scoped provided by the Spring Framework supports following five scopes: In singleton scope, Spring scopes the bean definition to a single instance per Spring IoC container. In prototype scope, a single bean definition has any number of object instances. In request scope, a bean is defined to an HTTP request. This scope is valid only in a web-aware Spring ApplicationContext. In session scope, a bean definition is scoped to an HTTP session. This scope is also valid only in a web-aware Spring ApplicationContext. In global-session scope, a bean definition is scoped to a global HTTP session. This is also a case used in a web-aware Spring ApplicationContext. The default scope of a Spring Bean is Singleton. 

6. Are Singleton beans thread safe in Spring Framework? 
No, singleton beans are not thread-safe in Spring framework. 

7. Explain Bean lifecycle in Spring framework:
 The spring container finds the bean’s definition from the XML file and instantiates the bean. Spring populates all of the properties as specified in the bean definition (DI). If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method. If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method. If the bean implements IntializingBean, its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called. If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called. If the bean implements DisposableBean, it will call the destroy() method. 
8. Which are the important beans lifecycle methods? 
Can you override them? 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. The bean tag has two important attributes (init-method and destroy-method) with which you can define your own custom initialization and destroy methods. There are also the correspondive annotations(@PostConstruct and @PreDestroy). 

9. What are inner beans in Spring? 
When a bean is only used as a property of another bean it can be declared as an inner bean. Spring’s XML-based configuration metadata provides the use of element inside the or elements of a bean definition, in order to define the so-called inner bean. Inner beans are always anonymous and they are always scoped as prototypes. 

10. How can you inject a Java Collection in Spring? 
 Spring offers the following types of collection configuration elements: The type is used for injecting a list of values, in the case that duplicates are allowed. The type is used for wiring a set of values but without any duplicates. The type is used to inject a collection of name-value pairs where name and value can be of any type. The type can be used to inject a collection of name-value pairs where the name and value are both Strings. 

11. What is bean wiring? 
Wiring, or else bean wiring is the case when beans are combined together within the Spring container. When wiring beans, the Spring container needs to know what beans are needed and how the container should use dependency injection to tie them together. 

12. What is bean auto wiring? 
 The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for a bean by inspecting the contents of the BeanFactory without using and elements. 

13. Explain different modes of auto wiring? 
The autowiring functionality has five modes which can be used to instruct Spring container to use autowiring for dependency injection: no: This is default setting. Explicit bean reference should be used for wiring. byName: When autowiring byName, the Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. byType: When autowiring by datatype, the Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exist, a fatal exception is thrown. constructor: This mode is similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. autodetect: Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType. 

14. Are there limitations with autowiring?
 Limitations of autowiring are: Overriding: You can still specify dependencies using and settings which will always override autowiring. Primitive data types: You cannot autowire simple properties such as primitives, Strings, and Classes. Confusing nature: Autowiring is less exact than explicit wiring, so if possible prefer using explicit wiring. 35. Can you inject null and empty string values in Spring? Yes, you can.

Next Post »

আপনার কোন কিছু জানার থাকলে কমেন্টস বক্স এ লিখতে পারেন। আমরা যথাযত চেস্টা করব আপনার সঠিক উত্তর দিতে। ভালো লাগলে ধন্যবাদ দিতে ভুলবেন না।
- শুভকামনায় ওয়েব স্কুল বিডি
ConversionConversion EmoticonEmoticon