Skip to Content, PHP, MySQL, Javascript, Ajax, Htacces

Spring Interview Questions and Answers For Freshers Part-3

Be First!

spring21.What is default scope of bean in Spring framework?
The default scope of bean is Singleton for Spring framework.

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

23.Explain Bean lifecycle in Spring framework?
Following is sequence of a bean lifecycle in Spring:

Instantiate – First the spring container finds the bean’s definition from the XML file and instantiates the bean..

Populate properties – Using the dependency injection, spring populates all of the properties as specified in the bean definition..

Set Bean Name – If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method.

Set Bean factory – If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method.

Pre Initialization – Also called postprocess of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method.

Initialize beans – If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called.

Post Initialization – If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

Ready to use – Now the bean is ready to use by the application.

Destroy – If the bean implements DisposableBean , it will call the destroy() method .

24.What are inner beans in Spring?
A <bean/> element inside the <property/> or <constructor-arg/> elements defines a so-called inner bean. An inner bean definition does not require a defined id or name; the container ignores these values. It also ignores the scope flag. Inner beans are always anonymous and they are always scoped as prototypes.

25.How can you inject Java Collection in Spring?
Spring offers four types of collection configuration elements which are as follows:

<list>: This helps in wiring i.e. injecting a list of values, allowing duplicates.

<set>: This helps in wiring a set of values but without any duplicates.

<map>: This can be used to inject a collection of name-value pairs where name and value can be of any type.

<props>: This can be used to inject a collection of name-value pairs where the name and value are both Strings.

26.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 your bean by inspecting the contents of the BeanFactory without using <constructor-arg> and <property> elements.

27.What are 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 which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter.

byName: Autowiring by property name. 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: Autowiring by property datatype. 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: 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.

28.What are the limitations with autowiring?
Limitations of autowiring are:

Overriding possibility: You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring.

Primitive data types: You cannot autowire so-called simple properties such as primitives, Strings, and Classes.

Confusing nature: Autowiring is less exact than explicit wiring, so if possible prefer using explicit wiring.

29.Can you inject null and empty string values in Spring?

30.What is Annotation-based container configuration?
An alternative to XML setups is provided by annotation-based configuration which relies on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.



Leave a Reply