Spring Interview Questions and Answers For Freshers Part-2
Bean Factory container: This is the simplest container providing basic support for DI .The BeanFactory is usually preferred where the resources are limited like mobile devices or applet based applications
Spring ApplicationContext Container: This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners.
12.Give an example of BeanFactory implementation.
The most commonly used BeanFactory implementation is the XmlBeanFactory class. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.
13.What are the common implementations of the ApplicationContext?
The three commonly used implementation of ‘Application Context’ are:
FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
ClassPathXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.
14.What is the difference between Bean Factory and ApplicationContext?
Following are some of the differences:
Application contexts provide a means for resolving text messages, including support for i18n of those messages.
Application contexts provide a generic way to load file resources, such as images.
Application contexts can publish events to beans that are registered as listeners.
Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context.
The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.
15.What are Spring beans?
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions.
16.What does a bean definition contain?
The bean definition contains the information called configuration metadata which is needed for the container to know the followings:
How to create a bean
Bean’s lifecycle details
17.How do you provide configuration metadata to the Spring Container?
There are following three important methods to provide configuration metadata to the Spring Container:
XML based configuration file.
18.How do add a bean in spring application?
Check the following example:
<?xml version=”1.0″ encoding=”UTF-8″?>
<bean id=”helloWorld” class=”com.tutorialspoint.HelloWorld”>
<property name=”message” value=”Hello World!”/>
19.How do you define a bean scope?
When defining a <bean> in Spring, you have the option of declaring a scope for that bean. For example, to force Spring to produce a new bean instance each time one is needed, you should declare the bean’s scope attribute to be prototype. Similar way if you want Spring to return the same bean instance each time one is needed, you should declare the bean’s scope attribute to be singleton.
20.What bean scopes does Spring support? Explain them.
The Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext.
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.