3

I am beginner in Spring MVC and a question that I have is why the beans are used.

As I got to know beans only have private variables with getters and setters.

I have few questions,

is that the same beans appear in Spring MVC,

why beans are used in Spring what is the syntax for defining beans

(my project is sampleSpr) sampleSpr-servlet.xml (which is in WEB-INF)

can someone help me to solve these questions please.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
bula
  • 8,719
  • 5
  • 27
  • 44

1 Answers1

3

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 definitions.

More to learn about beans and scope from SpringSource:

When you create a bean definition what you are actually creating is a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, just like a class, you can potentially have many object instances created from a single recipe.

You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition, but also the scope of the objects created from a particular bean definition. This approach is very powerful and gives you the flexibility to choose the scope of the objects you create through configuration instead of having to 'bake in' the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes

beans is the namespace prefix for the Spring beans XML schema. The mapping of namespace prefix to namespace in schema definition is done elsewhere--most likely in the root element.

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

no prefix is necessary because the default namespace is mapped to the same schema--again, most likely in the root element. From the documentation:

<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.xsd">

The xmlns: means the default namespace is http://www.springframework.org/schema/beans. In the xsi:schemaLocation attribute, you see the namespace is mapped to the Spring beans schema where that namespace is defined:

<xsd:schema xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.springframework.org/schema/beans">
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62