Q1:
What are types of IoC containers? Explain them.
Ans: There are two types of IoC containers:
- 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.
· Q2: Give an example of BeanFactory implementation.
· Ans: 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.
· Q3: What are the common implementations of the ApplicationContext?
· Ans: 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.
Q4:
What is the difference between Bean Factory and ApplicationContext?
A: 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.
Q5:
What are Spring beans?
Ans: 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.
Q6:
What does a bean definition contain?
Ans: 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
- Bean's dependencies
Q7:
How do you provide configuration metadata to the Spring Container?
Ans: There are following three important
methods to provide configuration metadata to the Spring Container:
- XML based configuration file.
- Annotation-based configuration
- Java-based configuration
Q8:
How do add a bean in spring application?
Ans: Check the following example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello
World!"/>
</bean>
</beans>
Q9:
How do you define a bean scope?
Ans: 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 besingleton.
No comments :
Post a Comment